欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JavaWeb静态文件处理

程序员文章站 2024-01-18 19:45:16
...

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>
相关标签: 静态