extjs desktop js和css文件壓縮

[gzip]使用tk-filters壓縮Web頁面資源

目的:壓縮資源文件,減少流量,減少頁面加載時間

1 首先,下載tk-filters-1.0.1.zip

2 解壓這個tk-filters-1.0.1.zip壓縮文件 ;

3 將解壓後的文件tk-filters.jar放在項目的WEB-INF/lib/下

4 配置tk-filters.properties文件,註釋非常明確,不再贅述,修改完後放在classpath及src下

#
# This properties file controls the behavior of the various
# filters available in the Tacit Knowledge filters package.
#
# Each filter has its own set of configuration directives,
# prefixed with the filter name, that controls that specific
# filter's behavior
#
############################################################
#                         ClusterCheck
############################################################
# A frequent problem when clustering is that applications use the session
# in a non-clusterable way, so The "ClusterCheckFilter" instruments the 
# application server's sessions with checks to see if this is a problem.
ClusterCheckFilter.Enabled=false
# Its possible to check for modifications to session objects after
# they have been set in to the session. This is a problem for sessions
# that are replicated in a copy-on-write fashion
ClusterCheckFilter.UnsetModificationsCheck=false
# Its possible to check serialized size to ensure high performance clustering
ClusterCheckFilter.ByteSizeCheck=false
# Aggregate size is important for containers that serialize the whole session
# every time. An example would be a database-backed session store.
# An aggregate size limit will also cap the maximum RAM used by sessions,
# allowing you to quantify the RAM necessary for peak loads.
ClusterCheckFilter.AggregateByteSizeLimit=30720
# Attribute size is important because each time an attribute is put in a
# session, it has to be serialized and persisted to a cluster peer, or
# to a database (depending on clustering implementations). 
#Thus, very large
# session attributes will be a performance problem.
ClusterCheckFilter.AttributeByteSizeLimit=20480
# ClusterCheck errors can return a 500 error to the client
# in order to have "fail-fast" behavior, if this is turned on.
# This is good for test machines, but is usually too aggressive for 
production.ClusterCheckFilter.ClientError=false
#############################################################
#############################################################
#                          GZIPFilter
#############################################################
# A performance boost can be achieved by sending data from the application
# server to the client using the Gzip encoding. This incurs a small CPU
# cost to gain a large network benefit. The GZIPFilter, when enabled, 
# transparently Gzip encodes all data after it leaves the application,
# but before its transmitted to the client.
# WARNING: GZIPFilter is currently not recommended for production use.
#          It does not send all data under certain conditions.
GZIPFilter.Enabled=true
# Its possible for the GZIPFilter to log statistics about the compression
# ratios and byte savings it is achieving. This turns that on or off.
GZIPFilter.LogStats=false
#############################################################
#                          CacheFilter
#############################################################
# A server can send expiration headers to the client, enabling the client 
# to confidently cache certain pieces of static content. This eliminates 
# unnecessary conditional GETs from the client to validate the freshness of
# content. If the application is on a server that doesn't do that, this
# filter can be enabled and mapped to static content (images, javascript,
# css files, etc), potentially reducing network traffic a great deal.
CacheFilter.Enabled=true
# This is the number of minutes the client will wait before verifying the
# freshness of a piece of content.
CacheFilter.ExpirationMinutes=15
#############################################################

5 修改web.xml文件

<!--
		GZIPFilter壓縮定義 設置此項時tk-filters.properties的
                GZIPFilter.Enabled=true纔可用
	-->
	<filter>
		<filter-name>GZIPFilter</filter-name>
		<filter-class>com.tacitknowledge.filters.
                 gzipfilter.GZIPFilter</filter-class>
	</filter>
	<!-- GZIPFilter 設置自己想要壓縮的文件類型-->
	<filter-mapping>
		<filter-name>GZIPFilter</filter-name>
		<url-pattern>*.js</url-pattern>
	</filter-mapping>
	<!--
		<filter-mapping> <filter-name>GZIPFilter</filter-name>
		<url-pattern>*.jsp</url-pattern> </filter-mapping>
	-->
	<!--
		CacheFilter緩存定義 
                設置此項時tk-filters.properties的CacheFilte
                 r.Enabled=true纔可用
	-->
	<filter>
		<filter-name>CacheFilter</filter-name>
		<filter-class>com.tacitknowledge.filters.cache
                     .CacheHeaderFilter
               </filter-class>
	</filter>
	<!-- CacheFilter 設置自己想要緩存的文件類型-->
	<filter-mapping>
		<filter-name>CacheFilter</filter-name>
		<url-pattern>*.gif</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>CacheFilter</filter-name>
		<url-pattern>*.jpg</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>CacheFilter</filter-name>
		<url-pattern>*.png</url-pattern>
	</filter-mapping>
	<!--<filter-mapping>
		<filter-name>CacheFilter</filter-name>
		<url-pattern>*.js</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>CacheFilter</filter-name>
		<url-pattern>*.css</url-pattern>
	</filter-mapping>

6 測試

測試使用FireFox,以及FireBug。ext-all-debug.js爲1.3MB。壓縮後得到的大小僅爲272K,壓縮率達到了79%。整個desktop頁面1.6M,壓縮後爲349.6KB,壓縮率爲78.64%。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章