Sometimes we do not have access to static resource files (such as: html,js,css,image, etc.) when using SPRINGMVC. The main reason for this is in the Web. xml file We set the mapping path of the SPRINGMVC front controller
<servlet> <Servlet-name>spmvc</Servlet-name> <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class> <Init-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>Classpath:springmvc-web.xml</Param-value> </Init-param> <Load-on-startup>1</Load-on-startup> </servlet> <servlet-mapping> <Servlet-name>Spmvc</Servlet-name> <Url-pattern>/</Url-pattern> </servlet-mapping>
The <url-pattern>/</url-pattern> in the above code is equivalent to intercepting all requests in the project, thus causing the static resource file to be inaccessible
Workaround One: Add a resource mapping in Springmvc-web.xml
< mvc:resources mapping ="/resources/** " Location = "/resources/" /> Span style= "COLOR: #0000ff" >< mvc:resources mapping = "/images/**" location = "/images/" /> < mvc:resources mapping = "/js/**" location = "/js/" />
- Mapping: Map, two *, which represents all URLs under Map resources/, including sub-paths (that is, multiple/)
- Location: Local resource path, note must be a path under the WebApp root directory
So that we can directly access the static content under the folder, the following path:
Http://localhost:8090/firstapp/images/cookie.png
Http://localhost:8090/firstapp/js/jquery-1.11.2.js
Web-inf is a secure directory for Java WEB applications . The so-called security is the client cannot access, only the server can access the directory. If you want to access the files directly in the page, you must map the files you want to access through the Web. xml file to access them.
Of course, you must modify the resources map if you want to put it in Web-inf, the code in the configuration file is as follows
<mapping= "/js/**" location= "/web-inf/js/"/>
Workaround Two: Use the default static resource handling servlet to handle static resources (involving Spring-mvc.xml, Web. xml)
- To enable the default servlet in Spring-mvc.xml
< Mvc:default-servlet-handler />
- Modify the Web. xml file, and the following code must be in front of Dispatcherservlet
<servlet-mapping> <Servlet-name>Default</Servlet-name> <Url-pattern>*.js</Url-pattern> <Url-pattern>*.css</Url-pattern> <Url-pattern>/assets/* "</Url-pattern> <Url-pattern>/images/*</Url-pattern> </servlet-mapping>
Workaround three: Change the URL mapping of Dispatcherservlet to the form of *.do, so that access to the static resource files will not be intercepted
<servlet> <Servlet-name>Spmvc</Servlet-name> <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class> <Init-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>Classpath:springmvc-web.xml</Param-value> </Init-param> <Load-on-startup>1</Load-on-startup></servlet> <servlet-mapping> <Servlet-name>Spmvc</Servlet-name> <Url-pattern>*.do</Url-pattern></servlet-mapping>
[Liu Yang Java]_springmvc method static Resources _ 9th