SpringMVC访问静态资源实例讲解

林炳文Evankaka原创作品。转自https://blog.csdn.net/Evankaka/article/details/45647031

       本文要以一个例子来说明SpringMVC访问静态资源

      <mvc:resources 的使用方法:
<!--对静态资源文件的访问-->

<mvc:resources mapping="/images/**" location="/images/" />
    
    
        /images /**映射到 ResourceHttpRequestHandler 进行处理,location指定静态资源的位置.可以是web application根目录下、jar包里面,这样可以把静态资源压缩到jar包中。cache-period可以使得静态资源进行web cache
       如果出现下面的错误,可能是没有配置 <mvc:annotation-driven /> 的原因。
报错WARNING: No mapping found for HTTP request with URI [/mvc/user/findUser/lisi/770] in DispatcherServlet with name 'springMVC'

        使用 <mvc:resources/> 元素,把 mapping 的 URI 注册到 SimpleUrlHandlerMapping的urlMap 中,key 为 mapping 的 URI pattern值,而 value为 ResourceHttpRequestHandler,
这样就巧妙的把对静态资源的访问由 HandlerMapping 转到 ResourceHttpRequestHandler 处理并返回,所以就支持 classpath 目录, jar 包内静态资源的访问.
另外需要注意的一点是,不要对 SimpleUrlHandlerMapping 设置 defaultHandler. 因为对 static uri 的 defaultHandler 就是ResourceHttpRequestHandler,

否则无法处理static resources request.

下面用一个例子来说明用法

本文工程免费下载

1、在eclipse中新建一个web工程、

然后导入如下包:


2、配置web.xml


    
    
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns= "http://java.sun.com/xml/ns/javaee" xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  4. xsi:schemaLocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  5. id= "WebApp_ID" version= "3.0">
  6. <!-- <welcome-file-list>
  7. <welcome-file>index.html</welcome-file>
  8. <welcome-file>index.htm</welcome-file>
  9. <welcome-file>index.jsp</welcome-file>
  10. <welcome-file>default.html</welcome-file>
  11. <welcome-file>default.htm</welcome-file>
  12. <welcome-file>default.jsp</welcome-file>
  13. </welcome-file-list> -->
  14. <!-- SpringMVC的前端控制器 -->
  15. <servlet>
  16. <servlet-name>MyDispatcher </servlet-name>
  17. <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
  18. <!-- 加载配置文件路径 -->
  19. <init-param>
  20. <param-name>contextConfigLocation </param-name>
  21. <param-value>/WEB-INF/spring-servlet.xml </param-value>
  22. </init-param>
  23. <!-- 何时启动 大于0的值表示容器启动时初始化此servlet,正值越小优先级越高-->
  24. <load-on-startup>1 </load-on-startup>
  25. </servlet>
  26. <!-- Spring MVC配置文件结束 -->
  27. <!-- SpringMVC拦截设置 -->
  28. <servlet-mapping>
  29. <servlet-name>MyDispatcher </servlet-name>
  30. <!-- 由SpringMVC拦截所有请求 -->
  31. <url-pattern>/ </url-pattern>
  32. </servlet-mapping>
  33. <!-- SpringMVC拦截设置结束 -->
  34. </web-app>

3、然后是控制器:


    
    
  1. package com.mucfc;
  2. import javax.servlet.http.HttpServletRequest;
  3. import javax.servlet.http.HttpServletResponse;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.ui.ModelMap;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.servlet.ModelAndView;
  8. @Controller
  9. public class StaticFileController {
  10. @RequestMapping(value= "/image/test")
  11. public ModelAndView img(HttpServletRequest request,HttpServletResponse response){
  12. System.out.println( "-----img-------");
  13. return new ModelAndView( "image");
  14. }
  15. @RequestMapping(value={ "/index", "/"}) //相对于根目录的路径
  16. public String test2() {
  17. return "index"; //指定页面要跳转的view视图路径
  18. }
  19. }

4、启动注解:


    
    
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:context= "http://www.springframework.org/schema/context"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p"
  4. xmlns:mvc= "http://www.springframework.org/schema/mvc"
  5. xsi:schemaLocation= "
  6. http://www.springframework.org/schema/mvc
  7. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
  8. http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  14. <!-- 把标记了@Controller注解的类转换为bean -->
  15. <context:component-scan base-package="com.mucfc"/>
  16. <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
  17. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
  18. <!-- 静态资源访问(不拦截此目录下的东西的访问) -->
  19. <mvc:annotation-driven />
  20. <mvc:resources location="/img/" mapping="/img/**" />
  21. <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
  22. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  23. p:prefix= "/WEB-INF/views/" p:suffix= ".jsp"/>
  24. </beans>
5、在WEB-INF新建一个目录views,并添加一个index.jsp和image.jsp

其中index.jsp:


    
    
  1. <%@ page language="java" contentType="text/html; charset=gb2312"
  2. pageEncoding= "gb2312"%>
  3. <!-- 这段代码的意思是获取当前项目的路径,如:http://localhost:8080/项目名称。 -->
  4. <%
  5. String path = request.getContextPath();
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  7. %>
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  9. <html>
  10. <head>
  11. <base href="<%=basePath%>">
  12. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  13. <title>Insert title here </title>
  14. </head>
  15. <body>
  16. 采用String返回视图:
  17. <a href="image/test">访问图片 </a>
  18. </body>
  19. </html>

和image.jsp如下内容:


    
    
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding= "UTF-8"%>
  3. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  4. <%
  5. String path = request.getContextPath();
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  7. %>
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  9. <html>
  10. <head>
  11. <base href="<%=basePath%>">
  12. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  13. <title>Insert title here </title>
  14. </head>
  15. <body>
  16. <h>图片 </h>
  17. <br/>
  18. <div>
  19. <img alt="图片" src="img/Koala.jpg">
  20. </div>
  21. </body>
  22. </html>

6、在WebContent新建一个目录,取名img,专门用来存放图片:

把图片考进去。

整个工程目录 如下:



8、接下来就运行了:

可以通过这里点击进去看图片,也可以直接输入http://localhost:8080/SpringMVCLearningChapter2_1/image/test

本文工程免费下载


林炳文Evankaka原创作品。转自https://blog.csdn.net/Evankaka/article/details/45647031

       本文要以一个例子来说明SpringMVC访问静态资源

      <mvc:resources 的使用方法:
<!--对静态资源文件的访问-->

<mvc:resources mapping="/images/**" location="/images/" />
  
  
        /images /**映射到 ResourceHttpRequestHandler 进行处理,location指定静态资源的位置.可以是web application根目录下、jar包里面,这样可以把静态资源压缩到jar包中。cache-period可以使得静态资源进行web cache
       如果出现下面的错误,可能是没有配置 <mvc:annotation-driven /> 的原因。
报错WARNING: No mapping found for HTTP request with URI [/mvc/user/findUser/lisi/770] in DispatcherServlet with name 'springMVC'

        使用 <mvc:resources/> 元素,把 mapping 的 URI 注册到 SimpleUrlHandlerMapping的urlMap 中,key 为 mapping 的 URI pattern值,而 value为 ResourceHttpRequestHandler,
这样就巧妙的把对静态资源的访问由 HandlerMapping 转到 ResourceHttpRequestHandler 处理并返回,所以就支持 classpath 目录, jar 包内静态资源的访问.
另外需要注意的一点是,不要对 SimpleUrlHandlerMapping 设置 defaultHandler. 因为对 static uri 的 defaultHandler 就是ResourceHttpRequestHandler,

否则无法处理static resources request.

下面用一个例子来说明用法

本文工程免费下载

1、在eclipse中新建一个web工程、

然后导入如下包:


2、配置web.xml


  
  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns= "http://java.sun.com/xml/ns/javaee" xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  4. xsi:schemaLocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  5. id= "WebApp_ID" version= "3.0">
  6. <!-- <welcome-file-list>
  7. <welcome-file>index.html</welcome-file>
  8. <welcome-file>index.htm</welcome-file>
  9. <welcome-file>index.jsp</welcome-file>
  10. <welcome-file>default.html</welcome-file>
  11. <welcome-file>default.htm</welcome-file>
  12. <welcome-file>default.jsp</welcome-file>
  13. </welcome-file-list> -->
  14. <!-- SpringMVC的前端控制器 -->
  15. <servlet>
  16. <servlet-name>MyDispatcher </servlet-name>
  17. <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
  18. <!-- 加载配置文件路径 -->
  19. <init-param>
  20. <param-name>contextConfigLocation </param-name>
  21. <param-value>/WEB-INF/spring-servlet.xml </param-value>
  22. </init-param>
  23. <!-- 何时启动 大于0的值表示容器启动时初始化此servlet,正值越小优先级越高-->
  24. <load-on-startup>1 </load-on-startup>
  25. </servlet>
  26. <!-- Spring MVC配置文件结束 -->
  27. <!-- SpringMVC拦截设置 -->
  28. <servlet-mapping>
  29. <servlet-name>MyDispatcher </servlet-name>
  30. <!-- 由SpringMVC拦截所有请求 -->
  31. <url-pattern>/ </url-pattern>
  32. </servlet-mapping>
  33. <!-- SpringMVC拦截设置结束 -->
  34. </web-app>

3、然后是控制器:


  
  
  1. package com.mucfc;
  2. import javax.servlet.http.HttpServletRequest;
  3. import javax.servlet.http.HttpServletResponse;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.ui.ModelMap;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.servlet.ModelAndView;
  8. @Controller
  9. public class StaticFileController {
  10. @RequestMapping(value= "/image/test")
  11. public ModelAndView img(HttpServletRequest request,HttpServletResponse response){
  12. System.out.println( "-----img-------");
  13. return new ModelAndView( "image");
  14. }
  15. @RequestMapping(value={ "/index", "/"}) //相对于根目录的路径
  16. public String test2() {
  17. return "index"; //指定页面要跳转的view视图路径
  18. }
  19. }

4、启动注解:


  
  
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:context= "http://www.springframework.org/schema/context"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p"
  4. xmlns:mvc= "http://www.springframework.org/schema/mvc"
  5. xsi:schemaLocation= "
  6. http://www.springframework.org/schema/mvc
  7. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
  8. http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  14. <!-- 把标记了@Controller注解的类转换为bean -->
  15. <context:component-scan base-package="com.mucfc"/>
  16. <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
  17. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
  18. <!-- 静态资源访问(不拦截此目录下的东西的访问) -->
  19. <mvc:annotation-driven />
  20. <mvc:resources location="/img/" mapping="/img/**" />
  21. <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
  22. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  23. p:prefix= "/WEB-INF/views/" p:suffix= ".jsp"/>
  24. </beans>
5、在WEB-INF新建一个目录views,并添加一个index.jsp和image.jsp

其中index.jsp:


  
  
  1. <%@ page language="java" contentType="text/html; charset=gb2312"
  2. pageEncoding= "gb2312"%>
  3. <!-- 这段代码的意思是获取当前项目的路径,如:http://localhost:8080/项目名称。 -->
  4. <%
  5. String path = request.getContextPath();
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  7. %>
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  9. <html>
  10. <head>
  11. <base href="<%=basePath%>">
  12. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  13. <title>Insert title here </title>
  14. </head>
  15. <body>
  16. 采用String返回视图:
  17. <a href="image/test">访问图片 </a>
  18. </body>
  19. </html>

和image.jsp如下内容:


  
  
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding= "UTF-8"%>
  3. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  4. <%
  5. String path = request.getContextPath();
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  7. %>
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  9. <html>
  10. <head>
  11. <base href="<%=basePath%>">
  12. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  13. <title>Insert title here </title>
  14. </head>
  15. <body>
  16. <h>图片 </h>
  17. <br/>
  18. <div>
  19. <img alt="图片" src="img/Koala.jpg">
  20. </div>
  21. </body>
  22. </html>

6、在WebContent新建一个目录,取名img,专门用来存放图片:

把图片考进去。

整个工程目录 如下:



8、接下来就运行了:

可以通过这里点击进去看图片,也可以直接输入http://localhost:8080/SpringMVCLearningChapter2_1/image/test

本文工程免费下载


猜你喜欢

转载自blog.csdn.net/weixin_42581112/article/details/88915476