【发布时间】:2013-01-17 08:02:38
【问题描述】:
我在使用 JSF 2 时遇到了一个问题。我将 Mojarra 2.1.14 与 Primefaces 3.1.4 一起使用
我有一个包含 2 个表单的页面:formA 和 formB。这两个表单在隐藏的输入字段中分别包含 ViewState。
<h:form id="formA" binding="#{sessionBean.formA}">
<h:commandButton value="formA" action="#{sessionBean.actionA}">
<f:ajax/>
</h:commandButton>
</h:form>
<h:form id="formB" binding="#{sessionBean.formB}">
<h:commandButton value="formB" action="#{sessionBean.actionB}">
<f:ajax/>
</h:commandButton>
</h:form>
用户使用 Ajax 操作提交 formA。在 Java 操作中,我显式更新了 formA 和 formB(已绑定)。
public void actionA(){
FacesContext.getCurrentInstance().getPartialViewContext().getRenderIds().add(formA.getClientId());
FacesContext.getCurrentInstance().getPartialViewContext().getRenderIds().add(formB.getClientId());
System.out.println("action A!!");
}
public void actionB(){
System.out.println("action B!!");
}
在 Ajax 响应中有 formA 和 formB(元素)和 ViewState 的 HTML 代码。
JSF更新formA和formB的HTML,并设置调用表单的ViewState:formA。 formB 不包含任何 ViewState。
用户使用 Ajax 操作提交 formB。因为未定义 ViewState,所以 postBack 为 false,并且在 RESTORE 阶段将 renderResponse 设置为 true,跳过了 INVOKE APPLICATION 阶段:未调用该操作。响应 VIEW_STATE 更新后,如果用户提交 formB,则调用该操作。
这是 JSF 2 的错误还是限制?还是我做错了什么?
你可以在 GitHub 上找到一个 maven 项目: https://github.com/nithril/jsf-multiple-form
提前感谢您的帮助!
【问题讨论】:
-
你能贴出这两个表单的代码,并用 AJAX 更新属性吗?
-
我即将完成一个小型 maven 项目
-
尝试在
f:ajax标签内显式添加render=":formB"。 -
它有效,但我还不明白为什么它与 getRenderIds().add(formB.getClientId()); 不同事实上,这两种形式都在 2 个 xhtml 文件片段中,彼此看不到。只有父组件可以看到 2 个表单。
标签: jsf-2 primefaces mojarra