JSF2:action和actionListener [英] JSF2: action and actionListener
问题描述
根据BalusC的回答,此处 action与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>,
.但是,当我决定编写一些代码进行测试时,结果会有所不同.这是我的小代码
From this answer by BalusC here 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>,
. However when I decide to write some code to test this, the result is a bit different. Here is my small code
<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
在这里更合适,但我的示例显示了否则.
According to BalusC, actionListener
is more suitable here, but my example show otherwise.
上面的代码在action
下可以很好地工作,但是如果我切换到actionListener
,那么它就不能正常工作.我需要两次单击才能使用actionListener
删除该表的条目,而如果使用action
,则每次单击该按钮都将删除条目.我想知道是否有JSF专家可以帮助我理解action
与actionListener
The above code work great with action
, but if I switch over to actionListener
, then it does not quite work. It will take two clicks for me to delete an entry of this table using actionListener
, while if I use action
, it delete entry every time I click the button. I wonder if any JSF expert out there can help me understand action
vs actionListener
注意:如果我切换到actionListener
,则我的delete
方法将变为public void delete(ActionEvent actionEvent)
Note If I switch to actionListener
, my delete
method become public void delete(ActionEvent actionEvent)
推荐答案
您正在将action
与actionListener
混淆. actionListener
始终在action
之前运行.如果有多个动作侦听器,则它们以与注册时相同的顺序运行.这就是为什么当您使用actionListener
调用业务操作并使用<f:setPropertyActionListener>
设置(准备)该业务操作要使用的属性时,它无法按预期工作的原因.此问题已在您之前的问题中解决并指出. Mojarra/MyFaces错误.
You're confusing action
with actionListener
. The actionListener
runs always before the action
. If there are multiple action listeners, then they run in the same order as they have been registered. That's why it doesn't work as expected when you use actionListener
to call the business action and <f:setPropertyActionListener>
to set (prepare) a property which is to be used by the business action. This problem was pointed out and fixed in your previous question Is this Primefaces bug or Mojarra/MyFaces bug.
delete()
方法中的所有内容显然都是业务操作,应改为由action
调用.业务操作通常会调用EJB服务,并且在必要时还会设置最终结果和/或导航到其他视图.
Whatever you have in the delete()
method is clearly a business action and should be invoked by action
instead. A business action typically invokes an EJB service and if necessary also sets the final result and/or navigates to a different view.
这篇关于JSF2:action和actionListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!