Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to call setPropertyActionListener before actionListener

Tags:

jsf

jsf-2

I'm currently experiencing a problem with JSF's order of execution.

Looking at my sample code:

<p:commandButton action="update.xhtml" ajax="false"
                        icon="ui-icon-pencil"
                        actionListener="#{marketingCodeBean.initForUpdate}">
    <f:setPropertyActionListener
        target="#{marketingCodeBean.marketingCode}" value="#{code}"></f:setPropertyActionListener>
</p:commandButton>

I would like to set a bean property using setPropertyActionListener, and do some processing on actionListener=initForUpdate. But JSF default sequence of execution is the opposite, actionListener first before setPropertyActionListener. Is there a clean work around for this problem?

I'm thinking of having an actionListener and pass the bean parameter to it, but I'm not sure if that is the best approach.

like image 324
czetsuya Avatar asked Jan 04 '13 15:01

czetsuya


1 Answers

That's indeed expected behaviour. The action listeners (actionListener, <f:actionListener> and <f:setPropertyActionListener>) are all invoked in the order they're registered on the component, with the actionListener attribute first. It's not possible to change the ordering this way other than adding the method behind actionListener as a <f:actionListener> (which should refer a concrete implementation class of ActionListener interface).

<p:commandButton ...>
    <f:setPropertyActionListener target="#{marketingCodeBean.marketingCode}" value="#{code}" />
    <f:actionListener type="com.example.InitForUpdate" />
</p:commandButton>

Better is to just use action instead of actionListener. It's invoked after all action listeners. Action listeners are intented to "prepare" an action and using them for business actions is actually poor practice.

<p:commandButton ... action="#{marketingCodeBean.initForUpdate}">
    <f:setPropertyActionListener target="#{marketingCodeBean.marketingCode}" value="#{code}" />
</p:commandButton>

with

public String initForUpdate() {
    // ...

    return "update.xhtml";
}

See also:

  • Differences between action and actionListener - explains when to use the one or the other.
like image 146
BalusC Avatar answered Oct 30 '22 08:10

BalusC