Multiple action listeners with a single command component in JSF

前端 未结 1 473
无人共我
无人共我 2021-01-02 17:14

Is it possible to invoke more than one listener method using a single command component? For example,

A view scoped bean:

@ManagedBean
@ViewScoped
pu         


        
相关标签:
1条回答
  • 2021-01-02 18:06

    Use <f:actionListener binding>:

    <h:commandButton value="Action">
        <f:actionListener binding="#{viewScopedBean.action()}"/>
        <f:actionListener binding="#{sessionScopedBean.action()}"/>
    </h:commandButton />
    

    Note the importance of the parentheses in EL. Omitting them would in this particular example otherwise throw a confusing javax.el.PropertyNotFoundException: Property 'action' not found on type com.example.ViewScopedBean, because it's by default interpreted as a value expression. Adding parentheses makes it a method expression. See also Why am I able to bind <f:actionListener> to an arbitrary method if it's not supported by JSF?

    You could even add an actionListener and/or an action method to the component the usual way, which is invoked later on. What it has to be unique is the action method, which decides the outcome for the processing.

    Anyway, keep in mind the listeners are always executed before the action and considered a "warming-up" for it. Your best is to perform the whole logic in the action method, even if you need to do bean injections.

    See also:

    • Call multiple backing bean methods at the same time
    • Differences between action and actionListener
    0 讨论(0)
提交回复
热议问题