• springmvc静态资源处理


    1.配置springmvc拦截规则,注意不能拦截 /*,这样的话,会对所有请求默认拦截,而应该拦截 /, 这样servlet会先走默认的拦截规则,默认拦截规则找不到后,才会走 / 这个规则,这样静态资源就可以正常处理.

    <!-- spring mvc -->
    <servlet>
       <servlet-name>springMvc</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring-mvc.xml</param-value>
       </init-param>
       <load-on-startup>1</load-on-startup>
       <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
       <servlet-name>springMvc</servlet-name>
       <url-pattern>/</url-pattern>
    </servlet-mapping>

    2. default servlet配置.利用tomcat的default servlet 处理规则,将所有的默认请求都由tomcat默认处理,这样静态资源tomcat就会先拦截输出

    <!-- The mapping for the default servlet -->
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>

    3.然后设置所有需要单独设置的资源请求

    <!-- 静态资源 -->
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.css</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.js</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.json</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.gif</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.tpl</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.png</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.jpg</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.ico</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.doc</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.xls</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.docx</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.xlsx</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.txt</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.swf</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.eot</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.svg</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.ttf</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.woff</url-pattern>
        </servlet-mapping>

    这样静态资源就能高效解决.

  • 相关阅读:
    CSUOJ 1081 集训队分组
    HDU 1865 More is Better
    HDU 1325 Is It A Tree?
    HDU 1272 小希的迷宫
    CSUOJ 1217 奇数个的那个数
    CSUOJ 1010 Water Drinking
    CSUOJ 1162 病毒
    CodeForces 295B
    CodeForces 20C
    SPOJ MULTQ3
  • 原文地址:https://www.cnblogs.com/sidesky/p/7715121.html
Copyright © 2020-2023  润新知