4 ways to pass parameter from JSF page to backing bean

As i know,there are 4 ways to pass a parameter value from JSF page to backing bean :

  1. Method expression (JSF 2.0)
  2. f:param
  3. f:attribute
  4. f:setPropertyActionListener

Let see example one by one :

1. Method expression

Since JSF 2.0, you are allow to pass parameter value in the method expression like this #{bean.method(param)}.

JSF page…

<h:commandButton action="#{user.editAction(delete)}" />

Backing bean…

@ManagedBean(name="user")
@SessionScoped
public class UserBean{
 
	public String editAction(String id) {
	  //id = "delete"
	}
 
}
Note
If you are deploy JSF application in servlet container like Tomcat, make sure you include the “el-impl-2.2.jar” properly. For detail, please read this article – JSF 2.0 method expression caused error in Tomcat.

2. f:param

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

JSF page…

<h:commandButton action="#{user.editAction}">
	<f:param name="action" value="delete" />
</h:commandButton>

Backing bean…

@ManagedBean(name="user")
@SessionScoped
public class UserBean{
 
	public String editAction() {
 
	  Map<String,String> params = 
                FacesContext.getExternalContext().getRequestParameterMap();
	  String action = params.get("action");
          //...
 
	}
 
}

See a full f:param example here.

3. f:atribute

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

JSF page…

<h:commandButton action="#{user.editAction}" actionListener="#{user.attrListener}"> 
	<f:attribute name="action" value="delete" />
</h:commandButton>

Backing bean…

@ManagedBean(name="user")
@SessionScoped
public class UserBean{
 
  String action;
 
  //action listener event
  public void attrListener(ActionEvent event){
 
	action = (String)event.getComponent().getAttributes().get("action");
 
  }
 
  public String editAction() {
	//...
  }	
 
}

See a full f:attribute example here.

4. f:setPropertyActionListener

Pass parameter value via f:setPropertyActionListener tag, it will set the value directly into your backing bean property.

JSF page…

<h:commandButton action="#{user.editAction}" >
    <f:setPropertyActionListener target="#{user.action}" value="delete" />
</h:commandButton>

Backing bean…

@ManagedBean(name="user")
@SessionScoped
public class UserBean{
 
	public String action;
 
	public void setAction(String action) {
		this.action = action;
	}
 
	public String editAction() {
	   //now action property contains "delete"
	}	
 
}

See a full f:setPropertyActionListener example here.

P.S Please share your idea, if you have any other ways :)

Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • ram

    before executing the page is showing error with red color below piece of code: Even The sample code i imported to my local machine ,it is not working plz get rid of me this problem

    My Server is : jboss-as-7.1.1.Final

    pom.xml is

    4.0.0
    com.mkyong.common
    JavaServerFaces
    war
    1.0-SNAPSHOT
    JavaServerFaces Maven Webapp
    http://maven.apache.org

    <!– For Java EE Application Server, uncomment this library
    and comment the rest of the libraries

    javax.faces
    jsf-api
    2.0
    provided

    –>


    com.sun.faces
    jsf-api
    2.1.0-b03

    com.sun.faces
    jsf-impl
    2.1.0-b03

    org.glassfish.web
    el-impl
    2.2

    javax.servlet
    jstl
    1.2

    javax.servlet
    javax.servlet-api
    3.0.1

    javax.servlet.jsp
    jsp-api
    2.1

    <!– too old

    com.sun.el
    el-ri
    1.0

    –>

    java.net.m2
    java.net m2 repo
    http://download.java.net/maven/2

    JavaServerFaces

    org.apache.maven.plugins
    maven-compiler-plugin
    2.3.1

    1.6
    1.6

  • http://[email protected] Bharat

    Map params =
    FacesContext.getExternalContext().getRequestParameterMap();

    Use this
    Map params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();

  • Kadir

    I am from Turkey. I am trying to develop a web site by using JSF (with PrimeFaces showcase) framework. But i am new for this subject. I find the answers to all the questions on JSF in your site. Congratulations for this. Thank you.

    • Vursana Lan

      AJ.. AJ.. AJEY.. Hey Yavrum Hey..

      44 > 23

      SAYGI

      Hey yavrum hey türke bak ingilizce yorum vay be Kadir

      • LOun

        VUR MUSTO VUR DUVARA.

  • mlblount45

    Can you update the first option to include the single quotes around the input parameter delete

  • tegoo

    I thing the 4th method is the best. Thank’s a lot for you. Also hidden forms may be used, but I don’t think it is a good idea.

    • tegoo

      *think

  • http://facebook.com/arturoagudelo18 Carlos Arturo

    Hello friends, greets, Please I need send a Object that represent a POJO with getters and setters to other xhtml and this can see it in a datatable, bone I have a object person so var=person from datatable and showit in other datatable sending to bean or from other way, Please helpme and exuse me my english thanks. to everyone

  • Caio

    Hello,

    how can I parameterize a list with an “a4j:commandButton”?

    thanks

    Caio.

  • http://facebook.com/shubhamkumarnayak Shubham
  • Holger

    Is method 1 still valid and if yes can complex types be used?

    e.g.

    &lt;h:commandButton action=&quot;#{user.addUser(user)}&quot; /&gt;
    @ManagedBean(name=&quot;user&quot;)
    @SessionScoped
    public class UserBean{
    	public String addUser(User user) {
    	  //do something with the user
    	}
    }

    Don’t seem to be able to get something like this working…

    Thanks

  • vijayan

    Please tell me about Difference between managed bean and backing bean?

    • tunnaruto

      There is not, they are the same thing

  • Maoduy

    I don’t know why i used the first way, and i got following error:

    javax.servlet.ServletException: /index.xhtml @47,103 action=”#{dbBean.getEditAction(list)}” Failed to parse the expression [#{dbBean.getEditAction(list)}]
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)

    Please help me!

  • saeed

    thank you very much.

  • LG

    Very nice post, Helpfull .
    Keep writing .

  • hannibaldaturn

    Is possible use for the example below:

    &lt;h:outputText id=&quot;field1&quot; value=&quot;#{myMB.methodWithParam('teste')}&quot; /&gt;

    or

    &lt;h:inputText id=&quot;field1&quot; value=&quot;#{myMB.methodWithParam('teste')}&quot; /&gt;

    ?

    Thanks for your post.

    • https://www.facebook.com/douglasnassif Douglas Junior

      I have the same question as you.

  • Gonzalo

    A simple bug

    Method 1
    Map params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();

  • http://www.yoleco.de Markus

    Hi. Thanks for the post! Helped me out allready TWICE.

  • Jim

    Examples 1,3 & 4 doesn’t work if the Bean’s uses the RequestScope. Did you any idea’s how to solve this without fetching the request param’s in the @PostConstruct method?

  • http://www.eduardunruh.de Eddy

    Thank you for this article! Really helped me :-)

  • curious

    Do they have advantage one over the other, i mean if 1 can why 4 ways to do the same thing.

  • http://www.kawoolutions.com/ Kawu

    The most complicated variant I’ve successfully used is a call including a parameter from within a ui:include’d sub view (Method 1 + ui:include):

    Client:

    &lt;ui:include src=&quot;...&quot;&gt;
      &lt;ui:param name=&quot;documentId&quot; value=&quot;#{doc.id}&quot; /&gt;
      &lt;ui:param name=&quot;acceptButtonBean&quot; value=&quot;#{repoHome}&quot; /&gt;
      &lt;ui:param name=&quot;acceptButtonAction&quot; value=&quot;removeIndividualDocument(documentId})&quot; /&gt;
    &lt;/ui:include&gt;

    Sub view:

    &lt;h:commandButton value=&quot;Continue&quot;
                     action=&quot;#{acceptButtonBean[acceptButtonAction]}&quot; /&gt;
      ...
    &lt;/h:commandButton&gt;

    This works great for lists that have a delete button to the right. HTH anybody

    • http://www.kawoolutions.com/ Kawu

      Please delete the above, it’s not working.

  • domgom

    Before of getting mad trying to make EL 2 working in tomcat I would suggest this minor change:

     

    Very nice post, bookmarked.

    • domgom

      The change is in method 1, using quotation marks in the parameter.
      action=”#{user.editAction(‘delete’)}

  • mightySephiroth

    Nice resume!! before reading, I just knew the method expression!! thx

  • Pingback: JSF 2.0 Tutorials()

  • henk

    There are actually at least two other ways for specific situations, though both not extremely pretty.

    Often the parameter you want to pass to a backing bean is an item from some iteration, e.g. a row from a data table. When your action method is executed, this row is directly available in the request scope under the name assigned to it on the page.

    E.g. consider this code:

    In your action method, you can now fetch “row” from the request scope and it will be the correct row on which the user clicked.

    In this example, yet another way to get to the same thing is to make sure “model” is some JSF DataModel. In your action method, the current row of this model instance will then also point to the correct row.

    All in all, I personally think method expressions with parameters are the clearest and most elegant solution.

    • Pio

      I can’t see the code!

    • fifeer

      Hi,

      Can you please write the code. I also tried to pass a value from an iteration to the action method but i couldn’t get the value in the action method. If i use a constant value as parameter, i can get it but if the parameter is from an iteration (dynamic value) then i can’t get it. (jsf 1.2 with icefaces 1.8.2).