提交2个表单时,JSF ViewState未更新

Nic*_*rot 5 primefaces jsf-2 mojarra

我对JSF 2有一个问题.我使用Mojarra 2.1.14和Primefaces 3.1.4

我有一个包含2种形式的页面:formAformB.这两个表单在隐藏的输入字段中包含每个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>
Run Code Online (Sandbox Code Playgroud)

用户使用Ajax操作提交formA.在Java操作中,我明确更新了formAformB(绑定了).

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!!");
}
Run Code Online (Sandbox Code Playgroud)

在Ajax响应中,有formAformB(元素)和ViewState 的HTML代码.

JSF更新formAformB的HTML 并设置调用表单的ViewState:formA. formB不包含任何ViewState.

用户使用Ajax操作提交formB.由于未定义ViewState,因此在RESTORE阶段将postBack设置为false并将renderResponse设置为true,跳过INVOKE APPLICATION阶段:不调用该操作.更新响应VIEW_STATE后,如果用户sumbit为formB,则调用该操作.

它是JSF 2的bug还是限制?或者我做错了什么?

你可以在GitHub上找到maven projet:https: //github.com/nithril/jsf-multiple-form

在此先感谢您的帮助!

par*_*lov 4

您面临的问题与 JSF JavaScript 库的已知问题有关。解决方法是在标签rendered的属性中显式设置另一种表单的客户端 ID f:ajax

<h:form id="formA" binding="#{sessionBean.formA}">
  <h:commandButton value="formA" action="#{sessionBean.actionA}">
    <f:ajax render=":formB @form"/>
  </h:commandButton>
</h:form>

<h:form id="formB" binding="#{sessionBean.formB}">
  <h:commandButton value="formB" action="#{sessionBean.actionB}">
    <f:ajax render=":formA @form"/>
  </h:commandButton>
</h:form>
Run Code Online (Sandbox Code Playgroud)

有关此的更多信息: