java 如何减少 JSF 中的 javax.faces.ViewState

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/121605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:05:50  来源:igfitidea点击:

How to reduce javax.faces.ViewState in JSF

javaoptimizationjsfviewstatebandwidth

提问by David Waters

What is the best way to reduce the size of the viewstate hidden field in JSF? I have noticed that my view state is approximately 40k this goes down to the client and back to the server on every request and response espically coming to the server this is a significant slowdown for the user.

减少 JSF 中视图状态隐藏字段大小的最佳方法是什么?我注意到我的视图状态大约是 40k,这会在每次请求和响应时返回到客户端并返回到服务器,特别是到达服务器,这对用户来说是一个显着的减速。

My Environment JSF 1.2, MyFaces, Tomcat, Tomahawk, RichFaces

我的环境 JSF 1.2、MyFaces、Tomcat、Tomahawk、RichFaces

采纳答案by Cristian Vat

Have you tried setting the state saving to server? This should only send an id to the client, and keep the full state on the server. Simply add the following to the file web.xml:

您是否尝试将状态保存到服务器?这应该只向客户端发送一个 id,并在服务器上保持完整状态。只需将以下内容添加到文件web.xml 中

 <context-param>
  <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
  <param-value>server</param-value>
 </context-param>

回答by David Waters

If you are using MyFaces you can try this setting to compress the viewstate before sending to the client.

如果您使用的是 MyFaces,您可以尝试使用此设置在发送到客户端之前压缩视图状态。

<context-param>
    <param-name>org.apache.myfaces.COMPRESS_STATE_IN_CLIENT</param-name>
    <param-value>true</param-value>
</context-param> `

回答by royalghost

One option is to completely save the view state on client side but you may face some problem such as not being able to Serialize the object. You may want to try using different compression algorithm/utility based on your requirement but since the browser will already use the GZip by default I am not sure how much you can gain.

一种选择是在客户端完全保存视图状态,但您可能会面临一些问题,例如无法序列化对象。您可能想根据您的要求尝试使用不同的压缩算法/实用程序,但由于浏览器默认情况下已经使用 GZip,我不确定您可以获得多少。