Java 为什么渲染响应阶段使用旧数据?
我有一个dataTable,每行都有一个commandButton,它使用当前项id从ManagedBean调用一个方法,如下所示:Java 为什么渲染响应阶段使用旧数据?,java,jsf,jsf-2,lifecycle,Java,Jsf,Jsf 2,Lifecycle,我有一个dataTable,每行都有一个commandButton,它使用当前项id从ManagedBean调用一个方法,如下所示: <h:commandButton action="#{myBean.doStuff(item.id)}" value="click me"/> 新的_项是我想要的,test1是我得到的 那么,为什么在渲染响应阶段调用doStuff方法,为什么使用错误的值呢 编辑:我正在使用JSF2.0,这通常不会有问题。例如,考虑下面的小面: <html xm
<h:commandButton action="#{myBean.doStuff(item.id)}" value="click me"/>
新的_项是我想要的,test1是我得到的
那么,为什么在渲染响应阶段调用doStuff方法,为什么使用错误的值呢
编辑:我正在使用JSF2.0,这通常不会有问题。例如,考虑下面的小面:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:body>
<h:form>
<h:dataTable value="#{tableBean.items}" var="item">
<h:column>
#{item}
</h:column>
<h:column>
<h:commandButton value="click" action="#{tableBean.doAction(item)}" />
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>
#{item}
以及以下支持bean:
@ManagedBean
@ViewScoped
public class TableBean {
private List<String> items;
@PostConstruct
public void init() {
items = new ArrayList<String>();
items.add("bar");
items.add("kaz");
items.add("foo");
}
public String doAction(String string) {
System.out.println(string);
return "";
}
public List<String> getItems() {
return items;
}
}
@ManagedBean
@视域
公共类TableBean{
私人清单项目;
@施工后
public void init(){
items=newarraylist();
项目。添加(“栏”);
项目。添加(“kaz”);
项目。添加(“foo”);
}
公共字符串doAction(字符串){
System.out.println(字符串);
返回“”;
}
公共列表getItems(){
退货项目;
}
}
这不会在渲染响应阶段打印任何额外内容。也许您需要显示更多的代码。此外,您还可以尝试将您的问题抽象为最基本的问题。如果它可以一步一步地工作,请重新添加其他代码,直到您看到它崩溃。谢谢您的建议。我创建了一个最小的示例,它按照预期工作。我现在把它缩小到坚持的问题。该错误仅在我持久化或合并特定对象时发生。希望我最终能解决这个问题。事实证明,在一个div上有一个onClick方法,它通过EL引用了myBean.doStuff,因此导致了对doStuff的额外调用。我更换了onClick,现在一切正常。
@ManagedBean
@ViewScoped
public class TableBean {
private List<String> items;
@PostConstruct
public void init() {
items = new ArrayList<String>();
items.add("bar");
items.add("kaz");
items.add("foo");
}
public String doAction(String string) {
System.out.println(string);
return "";
}
public List<String> getItems() {
return items;
}
}