17

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

查看我的示例代码:

<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之前。这个问题有干净的解决方法吗?

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

4

1 回答 1

33

这确实是预期的行为。动作监听器(actionListener<f:actionListener><f:setPropertyActionListener>都按照它们在组件上注册的顺序被调用,actionListener属性在前。actionListener除了将后面的方法添加为a <f:actionListener>(应该引用接口的具体实现类)之外,不可能以这种方式更改顺序ActionListener

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

更好的是只使用action而不是actionListener. 它在所有动作监听器之后被调用。动作侦听器旨在“准备”一个动作,并将它们用于业务动作实际上是不好的做法。

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

public String initForUpdate() {
    // ...

    return "update.xhtml";
}

也可以看看:

于 2013-01-04T15:30:14.943 回答