JSF2 Action parameter

23,872

Solution 1

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)

Solution 2

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.

Solution 3

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” *

Share:
23,872
Sven
Author by

Sven

Hi, I am a student from Germany. I set my focus on Web Application Development, especially with Java. And I hope this awesome site can help with the awkward behaviour I sometimes cause (: Have a nice day.

Updated on May 11, 2020

Comments

  • Sven
    Sven almost 4 years

    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?