1. 程式人生 > >JavaWeb靜態檔案處理

JavaWeb靜態檔案處理

Tomcat 處理靜態檔案快取


<filter>
 <filter-name>ExpiresFilter</filter-name>
 <filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
 <init-param>
  <param-name>ExpiresByType text/css</param-name>
  <param-value>access plus 1 day</param-value
>
</init-param> <init-param> <param-name>ExpiresByType application/javascript</param-name> <param-value>access plus 1 day</param-value> </init-param> </filter> <filter-mapping> <filter-name>ExpiresFilter</filter-name> <url-pattern
>
*.js</url-pattern> <dispatcher>REQUEST</dispatcher> </filter-mapping> <filter-mapping> <filter-name>ExpiresFilter</filter-name> <url-pattern>*.css</url-pattern> <dispatcher>REQUEST</dispatcher> </filter-mapping>

通過Maven進行靜態壓縮

<plugin>
 <artifactId>maven-war-plugin</artifactId>
 <version>2.4</version>
 <configuration>
  <warSourceExcludes>**/*.js,**/*.css</warSourceExcludes>
 </configuration>
</plugin>
<plugin>
 <groupId>net.alchim31.maven</groupId>
 <artifactId>yuicompressor-maven-plugin</artifactId>
 <version>1.3.0</version>
 <executions>
  <execution>
   <id>cpjscs</id>
   <phase>prepare-package</phase>
   <goals>
    <goal>compress</goal>
   </goals>
  </execution>
 </executions>
 <configuration>
  <!-- 包含js檔案和css檔案 -->
  <includes>
   <include>**/*.js</include>
   <include>**/*.css</include>
  </includes>
  <!-- 排除本來就壓縮的js檔案,比如jquery.min.js xxx-min.js之類的檔案 -->
  <excludes>
   <exclude>**/*.min.js</exclude>
   <exclude>**/*-min.js</exclude>
  </excludes>
  <encoding>utf8</encoding>
  <failOnWarning>false</failOnWarning>
  <force>true</force>
  <nosuffix>true</nosuffix>
  <linebreakpos>-1</linebreakpos>
  <sourceDirectory>${basedir}/webapp</sourceDirectory>
  <jswarn>false</jswarn>
 </configuration>
</plugin>