Java JSF2.0:为什么我的ViewScope bean被重新创建,即使它仍然在同一个视图上
在我的.xhtml页面中,我有以下表单:Java JSF2.0:为什么我的ViewScope bean被重新创建,即使它仍然在同一个视图上,java,exception,jsf-2,scope,Java,Exception,Jsf 2,Scope,在我的.xhtml页面中,我有以下表单: <ui:composition xmlns:ui="http://java.sun.com/jsf/facelets" template="./../template/CustomerTemplate.xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
template="./../template/CustomerTemplate.xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.prime.com.tr/ui">
<ui:define name="formContent">
<h:form>
<p:dataGrid var="item" value="#{mrBean.items}" columns="3">
<p:column>
<p:panel header="#{item.name}">
<h:panelGrid columns="1" style="width:100%">
...
<h:commandButton value="Add To Cart" actionListener="#{cartBean.addItem(item.id)}" />
...
</h:panelGrid>
</p:panel>
</p:column>
</p:dataGrid>
</h:form>
</ui:define>
</ui:composition>
有很多可能的原因,最终都归结为中所述的鸡蛋问题。您正在使用
将UI组件绑定到视图范围的托管bean属性,或者正在将标记处理程序的属性(如
,
等)绑定到视图范围的托管bean属性
这将在JSF2.2中修复。在此之前,您最好的选择是寻找替代方法或将上下文参数javax.faces.PARTIAL\u STATE\u SAVING
设置为false
另见:
commandButton
,但没有一个给我这个错误。无论如何,我通过将commandButton
包装在ajax
标记中,成功地实现了它。我更新了我的问题以反映它=)。你也可以把
放在
里面。我只建议将按钮的actionListener
替换为action
。至于最初的问题,如果您发布从
到
的尽可能最小的视图标记,这将更加有用,因为
仍然会重现问题。我尝试将actionListener
替换为action
,但没有帮助。我已经用你的建议更新了我的问题=],我不是这个意思。我的意思是宁愿做
。您的问题更新仅显示不完整的代码。它必须是尽可能小的复制、粘贴、运行的代码段,仍然会重现问题。您必须将现有代码复制粘贴到单个测试视图和bean中,然后逐步删除部分,直到问题不再出现,然后返回一步并发布它。此外,删除不相关的属性,如类、样式等,将有助于最大限度地减少代码片段。@BalusC在过去4年中,您多次救了我的命。。。非常感谢你。。。
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.prime.com.tr/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
... // import css, js files
</h:head>
<h:body>
... // Other things on the page
<div class="grid_9 content">
<ui:insert name="contentTitle"></ui:insert>
<ui:insert name="formContent"></ui:insert>
</div>
...
</h:body>
</html>
@ManagedBean
@ViewScoped
public class MrBean {
...
private List<ItemState> items;
...
@PostConstruct
public void prepareItemList() {
...
Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
partnerID = Long.parseLong(params.get("partnerID"));
...
}
}
...
<f:ajax listener="#{cartManagedBean.addItem(item.id)}">
<h:commandButton value="Add To Cart" />
</f:ajax>
....