【发布时间】:2012-01-30 18:39:44
【问题描述】:
来自 BalusC 的这个答案 Differences between action and actionListener, Use actionListener if you want have a hook before the real business action get executed, e.g. to log it, and/or to set an additional property (by <f:setPropertyActionListener>,。但是,当我决定编写一些代码来测试它时,结果有点不同。这是我的小代码
<h:form id="form">
<h:panelGroup id="mygroup">
<p:dataTable id="mytable" value="#{viewBean.foodList}" var="item">
<p:column>
#{item}
</p:column>
<p:column>
<p:commandButton value="delete"
action="#{viewBean.delete}"
update=":form:mygroup">
<f:setPropertyActionListener target="#{viewBean.selectedFood}"
value="#{item}"/>
</p:commandButton>
</p:column>
</p:dataTable>
</h:panelGroup>
</h:form>
这是我的豆子
@ManagedBean
@ViewScoped
public class ViewBean {
private List<String> foodList;
private String selectedFood;
@PostConstruct
public void init(){
foodList = new ArrayList<String>();
foodList.add("Pizza");
foodList.add("Pasta");
foodList.add("Hamburger");
}
public void delete(){
foodList.remove(selectedFood);
}
//setter, getter...
}
根据 BalusC,actionListener 在这里更合适,但我的示例显示否则。
上面的代码在action 上工作得很好,但是如果我切换到actionListener,那么它就不太工作了。使用actionListener 删除此表的条目需要单击两次,而如果我使用action,则每次单击按钮时都会删除条目。我想知道是否有任何 JSF 专家可以帮助我理解 action 与 actionListener
注意如果我切换到actionListener,我的delete方法变成public void delete(ActionEvent actionEvent)
【问题讨论】:
-
它是特定于 primefaces 的,还是与
<h:commandButton>相同? -
@mrembisz:我确实使用了
p:commandButton中的update,所以我认为有一个与primefaces 相关,但我不是100% 确定。将尝试深入研究。 -
@mrembisz:J4mes 先生确实确认
h:commandButton表现出相同的行为。
标签: jsf jsf-2 primefaces action actionlistener