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.
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";
}
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