日期:2014-05-16  浏览次数:20373 次

压缩你的ExtJs文件吧,它大的要命

ExtJs-all.js = 600K,

ExtJs-all.css = 200K.

?

其他累加>1M

?

敢问一下,如何与200K的WEB常规页面对抗速度?..

?

压缩你的ExtJs的JS文件吧.(我用的7zip,压缩成浏览支持的本地解压文件gzip格式)

?

然后写Filter过滤URL请求,解压补到的gzip文件.

?

通常,我把压好的js文件改名为Ext-all.gz.js(压缩默认为Ext-all.js.gz)

?

为了让浏览器识别gzip格式,我们要过滤到这种格式后,响应给浏览器解析时,要家上头文件格式.

?

下面代码:

package filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GzipFilter implements Filter {

	public void destroy() {
	}

	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		String url = ((HttpServletRequest)req).getServletPath();
		if(url.endsWith(".gz.js")||url.endsWith(".gz.css")){
			((HttpServletResponse)res).setHeader("Content-Encoding", "gzip");
		}
		chain.doFilter(req, res);
	}

	public void init(FilterConfig filterConfig) throws ServletException {
	}

}

?web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<filter>
		<filter-name>gzip-filter</filter-name>
		<filter-class>filter.GzipFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>gzip-filter</filter-name>
		<url-pattern>*</url-pattern>
	</filter-mapping>
	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

?

好了,快去试试吧.

?

我原本响应ext-all.js为600ms,压缩后文件为170K,响应为47ms,页面大小在200K左右.加上好的界面,怎么不能比一个常规网页?呵呵

?

压缩前

?

?

压缩后