本文介绍了是否可以在actionListener之前调用setPropertyActionListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前遇到JSF执行顺序的问题.

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

看我的示例代码:

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

我想使用setPropertyActionListener设置bean属性,并对actionListener = initForUpdate进行一些处理.但是JSF的默认执行顺序是相反的,actionListener首先在setPropertyActionListener之前.是否可以解决此问题?

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?

我正在考虑使用一个actionListener并将bean参数传递给它,但是我不确定这是否是最好的方法.

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.

推荐答案

这确实是预期的行为.动作侦听器(actionListener<f:actionListener><f:setPropertyActionListener>)全部以它们在组件上注册的顺序调用,首先具有actionListener属性.除了将actionListener后面的方法添加为<f:actionListener>(应引用 ActionListener 接口).

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>

更好的是只使用action而不是actionListener.在所有动作监听器之后 调用它.动作侦听器旨在准备"动作,而将其用于业务动作实际上是不明智的做法.

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>

使用

public String initForUpdate() {
    // ...

    return "update.xhtml";
}

另请参见:

  • action和actionListener之间的区别-说明何时使用一个或另一个.
  • See also:

    • Differences between action and actionListener - explains when to use the one or the other.
    • 这篇关于是否可以在actionListener之前调用setPropertyActionListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 20:00