[SpringMVC] SpringMVC accesses static resources and springmvc accesses static resources.
Original works of Lin bingwen Evankaka. Reprinted please indicate the source http://blog.csdn.net/evankaka
This document uses an example to illustrate SpringMVC's access to static resources.
<Mvc: resources usage:
<! -- Access static resource files -->
<mvc:resources mapping="/images/**" location="/images/" />
/Images/** ing to ResourceHttpRequestHandler for processing. location specifies the location of static resources. it can be under the web application root directory and in the jar package, so that static resources can be compressed into the jar package. Cache-period allows static resources to be used for web cache
If the following error occurs, it may be because <mvc: annotation-driven/> is not configured.
Error WARNING: No mapping found for HTTP request with URI [/mvc/user/findUser/lisi/770] in DispatcherServlet with name 'springmvc'
Use the <mvc: resources/> element to register the mapping URI to the urlMap of SimpleUrlHandlerMapping. The key is the URI pattern value of mapping, and the value is ResourceHttpRequestHandler,
In this way, the access to static resources is converted from HandlerMapping to ResourceHttpRequestHandler for processing and returning. Therefore, the classpath directory and the access to static Resources in the jar package are supported.
In addition, do not set defaultHandler for SimpleUrlHandlerMapping. Because the defaultHandler for static uri is ResourceHttpRequestHandler,
Otherwise, the static resources request cannot be processed.
The following example is used to describe the usage.
Free project download
1. Create a new web project in eclipse,
Then import the following package:
2. Configure web. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <Web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: web = "http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id = "WebApp_ID" version = "3.0"> <! -- <Welcome-file-list> <welcome-file> index.html </welcome-file> <welcome-file> index.htm </welcome-file> <welcome-file> index. jsp </welcome-file> <welcome-file> default.html </welcome-file> <welcome-file> default.htm </welcome-file> <welcome-file> default. jsp </welcome-file> </welcome-file-list> --> <! -- Front-end controller of SpringMVC --> <servlet-name> MyDispatcher </servlet-name> <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> <! -- Load the configuration file path --> <init-param> <param-name> contextConfigLocation </param-name> <param-value>/WEB-INF/spring-servlet.xml </param-value> </init-param> <! -- When to start a servlet whose value is greater than 0 indicates that the servlet is initialized when the container is started, the smaller the positive value, the higher the priority --> <load-on-startup> 1 </load-on-startup> </servlet> <! -- Spring MVC configuration file ended --> <! -- SpringMVC interception settings --> <servlet-mapping> <servlet-name> MyDispatcher </servlet-name> <! -- All requests are intercepted by SpringMVC --> <url-pattern>/</url-pattern> </servlet-mapping> <! -- SpringMVC interception settings ended --> </web-app>
3. Then the controller:
Package com. mucfc; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. springframework. stereotype. controller; import org. springframework. ui. modelMap; import org. springframework. web. bind. annotation. requestMapping; import org. springframework. web. servlet. modelAndView; @ Controllerpublic class StaticFileController {@ RequestMapping (value = "/image/test") public ModelAndView img (HttpServletRequest request, HttpServletResponse response) {System. out. println ("----- img -------"); return new ModelAndView ("image") ;}@ RequestMapping (value = {"/index ","/"}) // relative to the root directory path public String test2 () {return "index"; // specify the view path to jump }}
4. Start annotation:
<Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: context = "http://www.springframework.org/schema/context" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: p = "http://www.springframework.org/schema/p" xmlns: mvc = "http://www.springframework.org/schema/mvc" xsi: schemaLocation = "http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <! -- Convert the class marked with @ Controller annotation to bean --> <context: component-scan base-package = "com. mucfc"/> <! -- Start the annotation function of Spring MVC to map requests and POJO annotations --> <bean class = "org. springframework. web. servlet. mvc. annotation. AnnotationMethodHandlerAdapter"/> <! -- Static resource access (do not intercept access to things in this directory) --> <mvc: annotation-driven/> <mvc: resources location = "/img/" mapping = "/img/**"/> <! -- Resolve the Model View name, that is, add the prefix suffix --> <bean class = "org. springframework. web. servlet. view. internalResourceViewResolver "p: prefix ="/WEB-INF/views/"p: suffix = ". jsp "/> </beans>
5. Create a directory views in the WEB-INF and add an index. jsp and image. jsp
Index. jsp:
<% @ Page language = "java" contentType = "text/html; charset = gb2312" pageEncoding = "gb2312" %> <! -- This code indicates obtaining the path of the current project, for example, http: // localhost: 8080/project name. --> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
And image. jsp:
<% @ Page language = "java" contentType = "text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <% @ taglib prefix =" c "uri =" http://java.sun.com/jsp/jstl/core "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
6. Create a directory named img in WebContent to store images:
Test the image.
The project directory is as follows:
8. Run the following command:
You can click here to view the image, or enter http: // localhost: 8080/SpringMVCLearningChapter2_1/image/test
Free project download
Original works of Lin bingwen Evankaka. Reprinted please indicate the source http://blog.csdn.net/evankaka