SpringMVC - to solve the problem can not access static resources

Reason: DispatcherServlet cause the client can not access any static resources directly


method one:

Using <MVC: Resources /> , for example:

  1. In the Spring-web.xml configuration file configuration, so the deal with static resources returned to the Web application server process via the Spring MVC framework , can achieve the purpose of access to static resources.
 <mvc:resources location="/img/" mapping="/img/**"/>   
 <mvc:resources location="/js/" mapping="/js/**"/>    
 <mvc:resources location="/css/" mapping="/css/**"/>  

Method Two:**

采用 < mvc:default-servlet-handler></mvc:default-servlet-handler>

  1. In the Spring-web.xml configuration file configuration:
<mvc:default-servlet-handler></mvc:default-servlet-handler>
  • If the request is a static resource, it forwards the request from the default Web server Servlet application process , if not static resource request before continuing to process the DispatcherServlet.
  • The default Servlet is default, so configure the default Servlet in web.xml under WEB-INF years
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/assets/*</url-pattern>
    <url-pattern>/js/*</url-pattern>
    <url-pattern>/vendor/*</url-pattern>
    <url-pattern>*.js</url-pattern>
    <url-pattern>*.jpg</url-pattern>
    <url-pattern>*.gif</url-pattern>
    <url-pattern>*.png</url-pattern>
    <url-pattern>*.css</url-pattern>
  </servlet-mapping>
  • Static resources url-pattern inside can be accessed directly

Note: To reduce the pressure of Web application server, it is best to use the second, referred to Servlet process.
Published 58 original articles · won praise 7 · views 9238

Guess you like

Origin blog.csdn.net/Mr_OO/article/details/103148684