JSF2 Action parameter

前端 未结 3 1807
名媛妹妹
名媛妹妹 2020-12-03 02:03

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 y

相关标签:
3条回答
  • 2020-12-03 02:31

    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)

    0 讨论(0)
  • 2020-12-03 02:34

    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.

    0 讨论(0)
  • 2020-12-03 02:47

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

    0 讨论(0)
提交回复
热议问题