I have read about passing parameters from jsf page to managedbean through actionListener. Is it also possible to pass a parameter to a simple action method?
Thank you for reading...
Thank you both for your advices! I would be lost without you :-)
Following worked for me:
<h:commandLink id="link" action="#{overviewController.showDetails}" >
<f:setPropertyActionListener target="#{overviewController.show_id}" value="#{project.id}" />
<h:outputText value="#{project.title}" />
</h:commandLink>
So now who deserves the green tick? :-P can I give two of them?
Yes. Either:
action="#{bean.method(param)}"
Or
<h:commandButton .. >
<f:setPropertyActionListener
target="#{bean.targetProperty}" value="#{param}" />
</h:commandbutton>
(and use the bean property in the method)
You're talking about parameters in this form?
<h:commandButton action="#{bean.action(param)}" />
That depends on the EL implementation. Only JBoss EL and JSP 2.2 EL is capable of doing this. How to install JBoss EL is described in this answer.
Alternatively, you can also just use f:param
. The f:param
used to work with h:commandLink
only, but since JSF 2.0 it also works on h:commandButton
. E.g.
<h:commandButton action="#{bean.action}">
<f:param name="foo" value="bar" />
</h:commandButton>
with a @ManagedProperty
which sets the parameter as managed bean property:
@ManagedProperty("#{param.foo}")
private String foo;
With this you're however limited to standard types (String
, Number
, Boolean
). An alternative is the f:setPropertyActionListener
:
<h:commandButton action="#{bean.action}">
<f:setPropertyActionListener target="#{bean.foo}" value="#{otherBean.complexObject}" />
</h:commandButton>
That said, there are more ways as well, but this all depends on the sole functional requirement and the bean scopes. Probably you don't need to pass a "parameter" at all after all.
The new spec. JSF2 allows that the action method receives a param so you be able to do
<h:commandButton action="#{bean.action(otherBean.complexObject)}">
at the ManagedBean the method will be:
public String action(Object complexObject)
*Note: make sure you include the “el-impl-2.2.jar” *
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With