However, the use of the first problem encountered is that, Extjs that huge head. Think of ways to reduce the size of ExtJS become the first problem to be solved.
To talk about my solution,拍砖welcome. Suddenly discovered that the ads posted in front of the locked
1, compressed confusion
ExtJS apart from the big man, the much quoted in other js libraries, projects and so on its own js file. OPOA component used to develop the final document will increase the total amount of js. The project so late to the merger of these compressed files. Js popular now there is a lot of compression tools, such as packer, jsMin, Esc, JSA, yui-compressor and so on. After the election, I actually used the yui-compressor.
yui-compressor Project Address: http://developer.yahoo.com/yui/compressor/
Download after the development of a java package jar. Based on the command line to use:
java-jar yuicompressor-xyzjar [options] [input file]
Js file in the Development of a manual can not be compressed, where the use ANT. Construction of the project can help you achieve the above mission.
<property name="yuicompressor" value="${tools}/yuicompressor-2.3.6.jar" />
<apply executable="java" parallel="false" verbose="true" dest="${dist}/WebRoot">
<fileset dir="${dist}/WebRoot">
<include name="modules/*.js" />
<include name="js/*.js" />
</fileset>
<arg line="-jar" />
<arg path="${yuicompressor}" />
<arg line="--type js --charset UTF-8 -o" />
<targetfile />
<mapper type="glob" from="*.js" to="*-m.js" />
</apply>
<concat destfile="${dist}/WebRoot/js/base.js" encoding="UTF-8" >
<fileset dir="${dist}/WebRoot/js">
<include name="*-m.js" />
</fileset>
</concat>
<concat destfile="${dist}/WebRoot/modules/modules.js" encoding="UTF-8" >
<fileset dir="${dist}/WebRoot/modules">
<include name="*-m.js" />
</fileset>
</concat>
<delete>
<fileset dir="${dist}/WebRoot">
<include name="js/*.js"/>
<include name="modules/*.js"/>
<exclude name="modules/modules.js" />
<exclude name="js/base.js" />
</fileset>
</delete>
2, gzip compression.
Gzip compression坛子里have discussed two kinds,
First, some of the container (server) functionality provided, but limited to specific containers. Such as apache + tomcat or resin-pro version.
Are two pre-deployment manually gzip compression, with the servlet filter, which can achieve gzip, but reduces flexibility.
I have used their own implementation, using gzip servlet filter implementation. The following is my code reference online content.
package sh.blog.util.web.filter;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
import javax.servlet.ServletOutputStream;
public class CompressedStream extends ServletOutputStream {
private ServletOutputStream out;
private GZIPOutputStream gzip;
/**
* 指定压缩缓冲流
* @param 输出流到压缩
* @throws IOException if an error occurs with the {@link GZIPOutputStream}.
*/
public CompressedStream(ServletOutputStream out) throws IOException {
this.out = out;
reset();
}
/** @see ServletOutputStream * */
public void close() throws IOException {
gzip.close();
}
/** @see ServletOutputStream * */
public void flush() throws IOException {
gzip.flush();
}
/** @see ServletOutputStream * */
public void write(byte[] b) throws IOException {
write(b, 0, b.length);
}
/** @see ServletOutputStream * */
public void write(byte[] b, int off, int len) throws IOException {
gzip.write(b, off, len);
}
/** @see ServletOutputStream * */
public void write(int b) throws IOException {
gzip.write(b);
}
/**
* Resets the stream.
*
* @throws IOException if an I/O error occurs.
*/
public void reset() throws IOException {
gzip = new GZIPOutputStream(out);
}
}
package sh.blog.util.web.filter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
public class CompressionResponse extends HttpServletResponseWrapper {
protected HttpServletResponse response;
private ServletOutputStream out;
private CompressedStream compressedOut;
private PrintWriter writer;
protected int contentLength;
/**
* 创建一个新的被压缩响应给HTTP
*
* @param response the HTTP response to wrap.
* @throws IOException if an I/O error occurs.
*/
public CompressionResponse(HttpServletResponse response) throws IOException {
super(response);
this.response = response;
compressedOut = new CompressedStream(response.getOutputStream());
}
/**
* Ignore attempts to set the content length since the actual content length
* will be determined by the GZIP compression.
*
* @param len the content length
*/
public void setContentLength(int len) {
contentLength = len;
}
/** @see HttpServletResponse * */
public ServletOutputStream getOutputStream() throws IOException {
if (null == out) {
if (null != writer) {
throw new IllegalStateException("getWriter() has already been called on this response.");
}
out = compressedOut;
}
return out;
}
/** @see HttpServletResponse * */
public PrintWriter getWriter() throws IOException {
if (null == writer) {
if (null != out) {
throw new IllegalStateException("getOutputStream() has already been called on this response.");
}
writer = new PrintWriter(compressedOut);
}
return writer;
}
/** @see HttpServletResponse * */
public void flushBuffer() {
try {
if (writer != null) {
writer.flush();
} else if (out != null) {
out.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/** @see HttpServletResponse * */
public void reset() {
super.reset();
try {
compressedOut.reset();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/** @see HttpServletResponse * */
public void resetBuffer() {
super.resetBuffer();
try {
compressedOut.reset();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Finishes writing the compressed data to the output stream. Note: this
* closes the underlying output stream.
*
* @throws IOException if an I/O error occurs.
*/
public void close() throws IOException {
compressedOut.close();
}
}
/**
* 如果浏览器支持解压缩,则压缩该代码
* @throws IOException ServletException if an error occurs with the {@link GZIPOutputStream}.
* 如果需要开启该过滤器,在web.xml中加入此代码
<filter>
<filter-name>gzip</filter-name>
<filter-class>com.strongit.finance.gzip</filter-class>
</filter>
<filter-mapping>
<filter-name>gzip</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
*/
package sh.blog.util.web.filter;
import java.io.IOException;
import java.util.Enumeration;
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;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class CompressionFilter implements Filter {
protected Log log = LogFactory.getFactory().getInstance(this.getClass().getName());
@SuppressWarnings("unchecked")
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
boolean compress = false;
if (request instanceof HttpServletRequest){
HttpServletRequest httpRequest = (HttpServletRequest) request;
Enumeration headers = httpRequest.getHeaders("Accept-Encoding");
while (headers.hasMoreElements()){
String value = (String) headers.nextElement();
if (value.indexOf("gzip") != -1){
compress = true;
}
}
}
if (compress){//如果浏览器支持则压缩
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.addHeader("Content-Encoding", "gzip");
CompressionResponse compressionResponse= new CompressionResponse(httpResponse);
chain.doFilter(request, compressionResponse);
compressionResponse.close();
}
else{//如果浏览器不支持则不压缩
chain.doFilter(request, response);
}
}
public void init(FilterConfig config) throws ServletException {
}
public void destroy(){
}
}
The effect of actual use to As an example
Landing window:
Home:








Responses to “Js compression of ExtJS development articles”