JSF 太多 commandLinks (h:form) 导致 ViewExpiredException

JSF too many commandLinks (h:form) lead to ViewExpiredException

我有一个 JSF 应用程序,它创建并显示大约 50 个报告。这些报告以 PNG 格式呈现,并在图片下方显示 table。

此 table 使用带有 switchType="client" 的 RichFaces 4 togglePanel。 togglePanel 仅用于折叠和展开 table。

<h:form>
        <rich:togglePanel id="#{param.reportWrapper}_togglePanel"
            stateOrder="opened,closed" activeItem="opened" switchType="client">
            <rich:togglePanelItem name="closed">
                <h:panelGroup>
                    <div class="myclass">
                        <ul class="container-icons">
                            <li>
                                <h:commandLink styleClass="container-max" value="maximieren">
                                        <rich:toggleControl targetPanel="#{param.reportWrapper}_togglePanel" targetItem="@next" />
                                </h:commandLink>
                            </li>
                        </ul>
                        <h3>My Heading</h3>
                    </div>
                </h:panelGroup>
            </rich:togglePanelItem>
            <rich:togglePanelItem name="opened">
                <h:panelGroup>
                    <div class="myclass">
                        <ul class="container-icons">
                            <li>
                                <h:commandLink styleClass="container-min" value="minimieren">
                                        <rich:toggleControl targetPanel="#{param.reportWrapper}_togglePanel" targetItem="@prev" />
                                </h:commandLink>
                            </li>
                        </ul>
                        <h3>Another Heading</h3>
                        <div class="scrolling-table-content">
                            <rich:dataTable>
                                      // ...
                            </rich:dataTable>
                        </div>
                    </div>
                </h:panelGroup>
            </rich:togglePanelItem>
        </rich:togglePanel>
        </h:form>

问题是,我有时会在加载报告时收到 ViewExpiredExceptions。我的 numberOfLogicalViews 和 numberOfViewsInSession 是 14。我不想将它设置为 50,因为内存问题,因为它不应该是必要的,因为实际上同时只显示一个报告。

我试图删除 h:form 标签,这些标签被视为 logicalView。在我看来,togglePanel 不是项目,它需要表单,因为它的开关类型是客户端(不是服务器和 ajax,它们需要表单标签)。但是命令 link 确实需要 form 标签,因为如果我删除它,就会出现错误 "this link is disabled as it is not nested within a jsf form"。

所以我尝试用 commandButton 替换 commandLink。这首先工作得很好,表格不再是必需的。但不知何故,这种行为现在完全是随机的。有时 table 可以展开,有时单击展开按钮时没有任何反应。当我再次添加表单标签时,它工作正常,但没有解决我的 ViewExpiredException。

希望有人能帮助我...

感谢您的帮助!

Buntspecht

如果您只需要切换面板,您可以使用 <a4j:commandLink> 来限制执行范围(因此它不会提交整个表单)。或者您可以完全删除命令组件,只使用 togglePanel 的 JavaScript API:

<a onclick="#{rich:component('panelId')}.switchToItem(#{rich:component('panelId')}.nextItem())">Next</a>

非常感谢 Makhiel 的帮助。我终于设法用 commandButtons 解决方案解决了这个问题。我无法解释原因,但我的 togglePanelItems 的 ID 在不同的报告中重复了。

给每个 togglePanelItem 一个唯一的 ID,例如

<rich:togglePanelItem name="closed" id="#{param.reportWrapper}_opened">

<rich:togglePanelItem name="opened" id="#{param.reportWrapper}_closed">

解决了问题...

所以现在我们去掉了所有 h:form 标签,因此逻辑视图减少了大约 50 个! :)