我在动态包含的页面上有一个奇怪的行为
我的第一页有一个组合可供选择,包括:
<h:panelGrid columns="2">
<h:outputLabel value="Choose your page to include" />
<h:selectOneMenu id="pageList" value="#{anyPageBean.page}">
<f:selectItems value="#{anyPageBean.pageList}" var="w" itemValue="#{w}" itemLabel="#{w}" />
</h:selectOneMenu>
</h:panelGrid>
AnyPageBean只返回以下两个页面之一:pilotNoTemplate.xhtml或pipo.xhtml
@ManagedBean
@SessionScoped
public class AnyPageBean {
private String page;
private java.util.List<String> pageList;
public AnyPageBean() {
super();
pageList = new ArrayList<String>();
pageList.add("pilotNoTemplate.xhtml");
pageList.add("pipo.xhtml");
}
当我选择该页面时,pilotNoTemplate.xhtml会正确显示(pipo页面也是如此)。
但是当我从pilotNoTemplate.xhtml提交表单时,JSF认为提交为POST是可以的, 但是只有Phase1和Phase6跨越,为什么? 我有一个PhaseListener显示阶段,它打印:
INFO IN pour /jsf2-todo-001-dynamic-page-include-v2/any.faces ¤¤¤¤¤ POST
INFO AJAX BEFORE RESTORE_VIEW 1 for viewId=null
INFO AJAX AFTER RESTORE_VIEW 1 for viewId=/any.xhtml
INFO AJAX BEFORE RENDER_RESPONSE 6 for viewId=/any.xhtml
INFO AJAX AFTER RENDER_RESPONSE 6 for viewId=/any.xhtml
如果表单中填写了任何内容,则会重置...
SECOND提交是正确的,所有阶段都是跨越的:
INFO IN pour /jsf2-todo-001-dynamic-page-include-v2/any.faces ¤¤¤¤¤ POST
INFO AJAX BEFORE RESTORE_VIEW 1 for viewId=null
INFO AJAX AFTER RESTORE_VIEW 1 for viewId=/any.xhtml
INFO AJAX BEFORE APPLY_REQUEST_VALUES 2 for viewId=/any.xhtml
INFO AJAX AFTER APPLY_REQUEST_VALUES 2 for viewId=/any.xhtml
INFO AJAX BEFORE PROCESS_VALIDATIONS 3 for viewId=/any.xhtml
INFO AJAX AFTER PROCESS_VALIDATIONS 3 for viewId=/any.xhtml
INFO AJAX BEFORE UPDATE_MODEL_VALUES 4 for viewId=/any.xhtml
INFO AJAX AFTER UPDATE_MODEL_VALUES 4 for viewId=/any.xhtml
INFO AJAX BEFORE INVOKE_APPLICATION 5 for viewId=/any.xhtml
INFO AJAX AFTER INVOKE_APPLICATION 5 for viewId=/any.xhtml
INFO AJAX BEFORE RENDER_RESPONSE 6 for viewId=/any.xhtml
INFO AJAX AFTER RENDER_RESPONSE 6 for viewId=/any.xhtml
我的模板页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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:f="http://java.sun.com/jsf/core">
<h:head>
<meta http-equiv="Cache-Control" content="no-store" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</h:head>
<h:body>
<div>
<h:panelGroup id="globalMessages">
<h:messages globalOnly="false" styleClass="messages" />
</h:panelGroup>
<ui:insert name="content" />
</div>
</h:body>
</html>
合成页面,可以选择要包含的页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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:f="http://java.sun.com/jsf/core" >
<ui:composition template="/layout/template.xhtml">
<ui:define name="content">
<h:form id="chooseForm">
<h:panelGrid columns="2">
<h:outputLabel value="Choose your page to include" />
<h:selectOneMenu id="pageList" value="#{anyPageBean.page}">
<f:selectItems value="#{anyPageBean.pageList}" var="w" itemValue="#{w}" itemLabel="#{w}" />
</h:selectOneMenu>
</h:panelGrid>
<h:commandButton id="show" value="Show page">
<f:ajax event="click" execute="@form" render=":panelForDynaPage :globalMessages" />
</h:commandButton>
</h:form>
<h:panelGroup id="panelForDynaPage">
<ui:include id="includeContenu" src="#{anyPageBean.page}" />
</h:panelGroup>
</ui:define>
</ui:composition>
</html>
pilotNoTemplate.xhtml页面:
<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core">
<f:metadata>
<f:viewParam id="b" name="id" value="#{pilotAction.id}" />
</f:metadata>
<f:event id="a" listener="#{pilotAction.prepare}" type="preRenderView" />
<h:form id="formPilot">
<h:panelGrid columns="2">
<h:outputLabel value="#{appMsg['pilot.firstname']}" />
<h:inputText id="firstname" label="#{appMsg['pilot.firstname']}" value="#{pilotHolder.pilot.firstname}"
required="true" />
<h:outputLabel value="#{appMsg['pilot.lastname']}" />
<h:inputText id="lastname" label="#{appMsg['pilot.lastname']}" value="#{pilotHolder.pilot.lastname}" />
<h:outputLabel value="#{appMsg['pilot.age']}" />
<h:inputText id="age" label="#{appMsg['pilot.age']}" value="#{pilotHolder.pilot.age}" required="true"
validator="#{pilotAction.validateAge}" />
</h:panelGrid>
<h:commandButton id="merge" action="#{pilotAction.doMerge}"
value="#{pilotHolder.pilot.id ne null ? appMsg['pilot.update'] : appMsg['pilot.create']}">
<f:ajax id="ajaxm" event="click" execute="@form" render=":panelForDynaPage :globalMessages" />
</h:commandButton>
</h:form>
</ui:composition>
我注意为xhtml页面中的每个组件提供“id”, 因为没有它就有这样的问题,在我的情况下似乎是没有用的。 任何想法?
我见过其他人有同样的问题,你找到了解决办法吗?
答案 0 :(得分:0)
这是由JSF spec issue 790引起的,这是一个在JSF 2.0 / 2.1中出现的错误,并在即将推出的JSF 2.2中修复。基本上,当JSF ajax在当前<h:form>
之外更新一个组件,而该组件又包含另一个<h:form>
组件作为子组件时,其视图状态将不会更新(这是一个疏忽)。只有在ajax更新中显式指定表单组件时才会更新它。您理论上可以通过将<h:panelGroup id="panelForDynaPage">
替换为<h:form id="panelForDynaPage">
来解决它,但这在技术上是错误的标记。
您可以使用以下JS来修复此错误。此脚本将为ajax更新后未检索任何视图状态的表单创建javax.faces.ViewState
隐藏字段。
var ie = /*@cc_on!@*/false;
jsf.ajax.addOnEvent(function(data) {
if (data.status == "success") {
var viewState = document.getElementsByName("javax.faces.ViewState")[0].value;
for (var i = 0; i < document.forms.length; i++) {
var form = document.forms[i];
if (!hasViewState(form)) {
var hidden = document.createElement(ie ? "<input name='javax.faces.ViewState'>" : "input");
hidden.setAttribute("type", "hidden");
hidden.setAttribute("name", "javax.faces.ViewState");
hidden.setAttribute("value", viewState);
hidden.setAttribute("autocomplete", "off");
form.appendChild(hidden);
}
}
}
});
function hasViewState(form) {
for (var i = 0; i < form.elements.length; i++) {
if (form.elements[i].name == "javax.faces.ViewState") {
return true;
}
}
return false;
}