我正在 Tomcat 上开发一个 JSF Web 应用程序,计划在不久的将来使用 Seam,并且我想添加对网页和资源(即 Javascript 和 CSS 文件)的压缩。我知道 Java Web 中 GZIP 响应的三种方法:
使用 Ehcache GZIP 过滤器:它在 Appfuse 中使用,因此它可能很可靠,并且在应用之前会检查用户代理是否支持 GZIP,但它似乎对我们将使用的 Seam 存在问题 http://seamframework.org/Community/EHCacheGZipFilterIncompatibleWithSeam .
使用 pjl 过滤器。来自 stackoverflow 问题:Tomcat Compression Does Not Add a Content-Encoding: gzip in the Header ,看起来没有任何内存泄漏,但我不知道Seam是否有问题。
使用 Tomcat 的内置压缩 - 尽管它可能不提供内容编码(Tomcat 6.0.14 似乎工作正常,但您只能为不应应用用户代理压缩的内容提供黑名单。
有人有在 JSF-Seam 环境中使用这些方法的经验吗?哪个是“最佳”解决方案?
谢谢, 格伦
最佳答案
GZIP 过滤器将显着减少初始加载时间。
您还可以实现一个缓存过滤器,使屏幕性能与基于 JavaScript 的 UI ( https://stackoverflow.com/a/35567540/5076414 ) 保持一致。
对于客户端组件,您可以使用 Primefaces,它是基于 JQuery 的 UI。
在 JSF 中启用 GZIP 过滤器
只需将其添加到您的
web.xml
<filter>
<filter-name>gzipResponseFilter</filter-name>
<filter-class>org.omnifaces.filter.GzipResponseFilter</filter-class>
<init-param>
<description>The threshold size in bytes. Must be a number between 0 and 9999. Defaults to 150.</description>
<param-name>threshold</param-name>
<param-value>150</param-value>
</init-param>
<init-param>
<description>The mimetypes which needs to be compressed. Must be a commaseparated string. Defaults to the below values.</description>
<param-name>mimetypes</param-name>
<param-value>
text/plain, text/html, text/xml, text/css, text/javascript, text/csv, text/rtf,
application/xml, application/xhtml+xml, application/x-javascript, application/javascript, application/json,
image/svg+xml, image/gif, application/x-font-woff, application/font-woff2, image/png
</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>gzipResponseFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/</location>
</error-page>
以及以下内容给您
pom.xml
<dependency>
<groupId>org.omnifaces</groupId>
<artifactId>omnifaces</artifactId>
<version>1.11</version>
</dependency>
如何验证我的屏幕是否使用 gzip
要查看您的内容是否已使用 gzip 和缓存,请在 Google Chrome 浏览器中 -> 右键单击屏幕 -> 检查 -> 单击网络选项卡 -> 刷新屏幕。单击图像、图标、样式表,看看是否在响应标题中看到以下内容
Content-Encoding:gzip
如果元素的状态为 200
关于java - 对 JSF-Seam Web 应用程序页面进行 GZIP 的最佳方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/977944/