1, if you only configure the interception of URLs similar to *.do format, then access to the static resources is not a problem, as follows:
<!--SPRINGMVC Core distributor-- <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> < init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath *:config/dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</ load-on-startup> </servlet> <!--mapping *.do requests--- <servlet-mapping> < Servlet-name>dispatcherservlet</servlet-name> <url-pattern>*.do</url-pattern>
2. If the configuration intercepts all requests, the following:
<!--SPRINGMVC Core distributor-- <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> < init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath *:config/dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</ load-on-startup> </servlet> <!--map all requests-- <servlet-mapping> < Servlet-name>dispatcherservlet</servlet-name> <url-pattern>/</url-pattern>
Such a configuration, will cause JS files, CSS files, image files and other static resources can not be accessed.
1) Take a picture as an example and store a picture of a.png in the/IMG directory under the Web project root directory.
2) Access this image in the JSP page
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 " pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
3) access to this JSP via controller
Package Cn.luxh.app.controller;import Org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.RequestMapping; @Controller @requestmapping (value= "/res") public class Staticresourceaccesscontroller { @RequestMapping (value= "/staticres") public String Accessres () { SYSTEM.OUT.PRINTLN ("Access to static Resources"); return "Getstaticres"; }}
4) The results of the visit are as follows:
You can see that the picture cannot be accessed.
5) SPRINGMVC provides <mvc:resources/> tags to handle this problem, in the SPRINGMVC configuration file Dispatcher-servlet.xml do the following configuration:
<!--static resource access -<!--similar to:/img/** URL requested resources are found in the/img/directory <mvc:resources location= "/img /"mapping="/img/** "/>
6) After configuration, re-access, OK.
3, for convenience, you can put JS files, CSS files, picture files in the resource directory of the respective sub-directory, such as/resource/js,/resource/css,/resource/img. Then do a unified configuration on it.
<!--static resource access--><mvc:resources location= "/resource/**" mapping= "/resource/**"/>
Reprint Address: http://www.cnblogs.com/luxh/archive/2013/03/14/2959207.html
SPRINGMVC accessing static resources [GO]