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 :
- Method expression (JSF 2.0)
- f:param
- f:attribute
- 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" } }
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 :)

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
Hello,
how can I parameterize a list with an “a4j:commandButton”?
thanks
Caio.
Is method 1 still valid and if yes can complex types be used?
e.g.
Don’t seem to be able to get something like this working…
Thanks
Please tell me about Difference between managed bean and backing bean?
There is not, they are the same thing
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!
thank you very much.
Very nice post, Helpfull .
Keep writing .
Is possible use for the example below:
or
?
Thanks for your post.
I have the same question as you.
A simple bug
Method 1
Map params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
Hi. Thanks for the post! Helped me out allready TWICE.
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?
Thank you for this article! Really helped me :-)
Do they have advantage one over the other, i mean if 1 can why 4 ways to do the same thing.
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:
Sub view:
This works great for lists that have a delete button to the right. HTH anybody
Please delete the above, it’s not working.
Before of getting mad trying to make EL 2 working in tomcat I would suggest this minor change:
Very nice post, bookmarked.
The change is in method 1, using quotation marks in the parameter.
action=”#{user.editAction(‘delete’)}
Nice resume!! before reading, I just knew the method expression!! thx
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.
I can’t see the code!
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).