JSF 2查看过期的最奇怪的错误(更新 - 仅在将状态保存到服务器时发生)

Ben*_*Ben 4 ajax jsf jsf-2

错误是这样的 - 我的JSF应用程序中有几个表单.如果我在特定表单之外激活AJAX调用20次或更多次,我会得到一个"No Saved View State Could be found for the view identifier"例外.

更新1仅当状态保存在服务器上时才会发生.设置此选项时,不会发生此问题:

<context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)

更新结束1

例如,假设我的应用程序中有表单A,表单B和表单C:(执行应用程序非常复杂,我将尝试提供所有相关信息)

<h:form>
  <h:commandButton value="A">
    <f:ajax render="@form"/>
  <h:commandButton>
<h:form>

<h:form>
  <h:commandButton value="B">
    <f:ajax render="@form"/>
  <h:commandButton>
<h:form>

<h:form>
  <h:commandButton value="C">
    <f:ajax render="@form"/>
  <h:commandButton>
<h:form>
Run Code Online (Sandbox Code Playgroud)

一个更重要的因素,每个时间点只能看到一种形式,其他形式则有display:none.最后,所有bean都是会话作用域.

现在,以下单击将导致异常(对于每一行,最后一次单击会导致异常)

  1. Ax20,B
  2. Ax19,B,C
  3. Ax10,Cx10,B
  4. Bx5,Cx5,Bx5,Cx5,A

这不会导致异常:

  1. Ax18,B,C,A.

换句话说,如果在最后20次单击中未单击表单中的按钮,则在下次单击时,将No save view state引发异常.
相同形式的所有按钮都等同于表单,这意味着,如果form A有按钮A1,A2则以下内容不会导致异常:

  1. A1x20,A2
  2. A1x19,B,A2
  3. A1,A2x20,B,A1

任何的想法?!?这让我疯了.

Bal*_*usC 5

您已从单个页面上超过了会话限制中的视图数.默认情况下,限制为15,但可以通过上下文参数进行配置web.xml.从技术上讲,每个表单都是一个具有自己视图状态的独立视图.当您在触摸其他表单的同时连续ajax更新一个表单时,它们在服务器端的视图状态将缓慢但肯定会过期.

在客户端保存视图状态确实可以解决此问题,因为将存储服务器端会话中的任何内容.它只会使响应大小更大(现在带宽便宜).

如果你想在服务器端保持视图状态,那么你应该render使用单个ajax表单中的其他表单,以便它们的视图状态也会更新:

<h:form id="A">
  <h:commandButton value="A">
    <f:ajax render="@form :B :C"/>
  <h:commandButton>
<h:form>

<h:form id="B">
  <h:commandButton value="B">
    <f:ajax render="@form :A :C"/>
  <h:commandButton>
<h:form>

<h:form id="C">
  <h:commandButton value="C">
    <f:ajax render="@form :A :B"/>
  <h:commandButton>
<h:form>
Run Code Online (Sandbox Code Playgroud)