默认情况下,变量com.sun.faces.numberOfViewsInSession在web.xml中设置为15.此变量创建一个视图树,jsf在向后点击15次时检索特定视图.
例如,我有3个屏幕用于应用程序,我使用Firefox.我在第一个标签中进入第三个屏幕.在第二个选项卡中,我有第一个屏幕,然后单击下一个按钮,jsf检索第一个屏幕并转到第二个屏幕.
这个机制很棒,但它消耗了大量的内存(对于我来说会话需要25Mo),当你将这个数字乘以7000时,我需要175 Go的内存,这是不可能的.
所以我试着设置com.sun.faces.numberOfViewsInSession等于1(3Mo by session).
但是以我的例子为例,当我点击下一个按钮的第二个标签时,我得到了下一个错误:
javax.servlet.ServletException: viewId:/private/pages/data/dataView.faces - View /private/pages/data/dataView.faces could not be restored. at javax.faces.webapp.FacesServlet.service(FacesServlet.java:270) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:206) at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290) at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:388) at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at fr.generali.mezzo.front.commun.performance.filters.PerformanceFilter.doFilter(PerformanceFilter.java:72) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:173) at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:182) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:525) at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104) at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157) at org.apache.catalina.authenticator.SingleSignOn.invoke(SingleSignOn.java:420) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447) at java.lang.Thread.run(Thread.java:595)
此错误是合乎逻辑的,因为我的numberOfViewsInSession为1.
所以,我的问题是:
鉴于我知道视图中的数据(对于第二个选项卡),我该如何捕获异常并为我的用户创建新视图?
谢谢你的帮助.
解决方法
如果您想遵循该路径,我认为您不应该尝试捕获异常,而是利用JSF中的StateManager API.这允许您自定义JSF框架管理其视图状态的方式.
请注意,这是一个高级主题.或者在Ed Burns'(JSF spec lead)中的话:
View state management is a complex business,and few application developers will have to worry about customizing it.
也就是说,如果您还没有使用JSF 2.0,那么我强烈建议升级到此. JSF 2.0中的一个重要新功能是部分状态保存.这大大减少了存储状态所需的内存量.
还有另一个选项,我想你可能已经考虑过看到你对com.sun.faces.numberOfViewsInSession等参数的了解,就是在客户端上使用状态.这将视图状态存储在隐藏字段中,并且基本上为您提供了无限的内存,因为客户端用作分布式内存.当然,这会增加网络开销.在AJAX的情况下,这种开销可能很大,以至于无法考虑.