Thursday, November 10, 2011

How to: pass parameters to backing bean in JSF application

There is few methods to solve this task:

1. f:param - pass parameter value via f:param tag and get it back via request parameter in backing bean.

...
<h:commandbutton action="#{viewScope.documentView.handleSign}" binding="#{viewScope.documentView.pod}" partialsubmit="true" text="Sign">
   <f:param name="path" value="c:\\oracle\\middleware\\aaa">
</f:param></h:commandbutton>
...


@ManagedBean(name="documentView")
@ViewScoped
public class DocumentView{
    //...
    public String handleSign() {
        String path =     FacesContext.getExternalContext().getRequestParameterMap(.get("path");


    }
    //...
}


2. f:setPropertyActionListener - pass parameter to PropertyActionListener defined in your bean

...
<h:commandbutton actionlistener="#{viewScope.documentView.handleSign}" binding="#{viewScope.documentView.pod}" partialsubmit="true" text="Sign">
   <f:setPropertyActionListener target="#{
viewScope.documentView.podActionValue}" value="Podpisz" />
</f:param></h:commandbutton>
...


@ManagedBean(name="documentView")
@ViewScoped
public class DocumentView{
    //...

    String podActionValue;
    public void
setPodActionValue(String value) {
        String podActionvalue = value;
    }
    //...
}


3. f:atribute - Pass parameter value via f:atribute tag and get it back via action listener in backing bean.

 ...
<h:commandbutton actionlistener="#{viewScope.documentView.handleSign}" binding="#{viewScope.documentView.pod}" partialsubmit="true" text="Sign">
  
<f:param name="path" value="c:\\oracle\\middleware\\aaa">
</f:param></h:commandbutton>
...


@ManagedBean(name="documentView")
@ViewScoped
public class DocumentView{
    //...
    public void
handleSign(ActionEvent event) {
        String path = (String)event.getComponent().getAttributes().get("path");
    }
    //...
}


4. ADF way
   af:clientAttribute - You can use af:clientAttribute similar to f:attribute

   af:clientListener/serverListener - When we need add some client side processing in javascript we can pass parameter value from af:clientAttribute via af:clientListener to javascript, and next to af:serverListener tag. You can get values back in listener in backing bean. 

 ...
<af:commandbutton actionlistener="#{viewScope.documentView.handleSign}" binding="#{viewScope.documentView.pod}" partialsubmit="true" text="Sign">

   <af:clientAttribute name="path" value="c:\\oracle\\middleware\\aaa"/>
  
<af:serverListener type="customEvent" method="{viewScope.documentView.handleSign}"/>
   </
af:serverListener>
   <af:clientListener method="signButton" type="click"/>
</af:commandbutton>


<af:resource type="javascript>
function podpiszButton(evt) {
                        var src = evt.getSource();
                        var path = evt.getSource().getProperty('path');
                        component = evt.getSource();
                        AdfCustomEvent.queue(component, "customEvent", {spath:path}, true);
                        evt.cancel();
                    }

</af:resource>
...


@ManagedBean(name="documentView")
@ViewScoped
public class DocumentView{
    //...
    public void
handleSign(ClientEvent event) {
        String path = (String)event.getParameters().get("spath");
    }
    //...
}



5. MethodExpression - the simplest way available in JSF 2.0 version


...
<h:commandButton action="#{viewScope.documentView.handleSign('aaa')}" />
...


@ManagedBean(name="documentView")
@ViewScoped
public class DocumentView{
    //...

    public void handleSign(String sign) {
        this.sign = sign;
    }
    //...
}

7 comments: