任何人都知道什么可能导致JSF实例化一个新的viewScoped bean,即使viewstate id是相同的。
在我的viewScoped bean构造函数中,我将代码放在下面以打印javax.faces.ViewState的值:
// construtor
public MyViewScopeBeanJsfBean() {
System.out.println("===== In MyViewScopeBeanJsfBean Constructor Begin =====");
printViewState();
System.out.println("===== In MyViewScopeBeanJsfBean Constructor End =====");
}
private void printViewState() {
String[] viewStates = getHttpServletRequest().getParameterValues("javax.faces.ViewState");
if (viewStates != null) {
for (String viewState :viewStates) {
System.out.println("javax.faces.ViewState="+viewState);
}
}
else {
System.out.println("javax.faces.ViewState=null");
}
}
以下是我得到的结果:
JSF created the viewscoped bean for the 1st time:
===== In MyViewScopeBeanJsfBean Constructor Begin =====
javax.faces.ViewState=null
===== In MyViewScopeBeanJsfBean Constructor End =====
I stayed on the same page and clicked on a ajax link to inlcude a page into the current page.
===== In MyViewScopeBeanJsfBean Constructor Begin =====
javax.faces.ViewState=y25eUekk0MqNOV7cxixhe+YOI31giRnbuL1ad9X158PdRuir
===== In MyViewScopeBeanJsfBean Constructor End =====
I then click on a link In the newly included page to upload a file:
===== In MyViewScopeBeanJsfBean Constructor Begin =====
javax.faces.ViewState=y25eUekk0MqNOV7cxixhe+YOI31giRnbuL1ad9X158PdRuir
===== In MyViewScopeBeanJsfBean Constructor End =====
在每次点击时,我都会停留在同一页面上,您可以看到视图状态ID是相同的。但是为什么JSF会调用构造函数来创建一个viewcoped bean的新实例?
注意:我的主模板中有以下内容来纠正与ajaxly相关的viewscoped bean问题包括页面:
<h:outputScript library="omnifaces" name="fixviewstate.js" target="head" />