springmvc访问静态资源的springmvc.xml配置

一. 问题及需求

  由于Spring MVC的web.xml文件中关于DispatcherServlet拦截url的配置为"/",拦截了所有的请求,同时*.js,*.jpg等静态资源也被拦截了,导致运行时跳转后的页面无法加载图片资源,如下图所示。

web.xml:

1     <!-- 配置DispatcherServlet所要拦截的url -->
2     <servlet-mapping>
3         <servlet-name>springmvc</servlet-name>
4         <url-pattern>/</url-pattern>
5     </servlet-mapping>

 loginSucc.jsp:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Spring MVC欢迎页面</title>
 8 </head>
 9 <body>
10 返回信息:${msg}
11 <!-- 静态资源jpg -->
12 <img alt="静态资源图片" src="../image/20160726.jpg">
13 </body>
14 </html>

  需求:正常加载出图片资源。

二. 解决方案

   只需要修改springmvc.xml: 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:mvc="http://www.springframework.org/schema/mvc"
 5        xmlns:p="http://www.springframework.org/schema/p"
 6        xmlns:context="http://www.springframework.org/schema/context"
 7        xmlns:aop="http://www.springframework.org/schema/aop"
 8        xmlns:tx="http://www.springframework.org/schema/tx"
 9        xsi:schemaLocation="http://www.springframework.org/schema/beans
10             http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
11             http://www.springframework.org/schema/context 
12             http://www.springframework.org/schema/context/spring-context-4.0.xsd
13             http://www.springframework.org/schema/aop 
14             http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
15             http://www.springframework.org/schema/tx 
16             http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
17             http://www.springframework.org/schema/mvc 
18             http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
19             http://www.springframework.org/schema/context 
20             http://www.springframework.org/schema/context/spring-context-4.0.xsd">
21     
22     <!-- 自动注册组件 -->
23     <mvc:annotation-driven/>
24     
25     <!-- 通过扫描将带有@Controller注解的类交由spring容器管理并维护 -->
26     <context:component-scan base-package="com.pers"/>
27 
28     <!-- 配置视图解析器 如果不配置ViewResolver,Spring MVC默认使用org.springframework.web.servlet.view.InternalResourceViewResolver作为
29     ViewResolver,而且prefix和suffix都为空 -->
30     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
31         <property name="prefix" value="/WEB-INF/jsp/"></property>
32         <property name="suffix" value=".jsp"></property>
33     </bean>
34     
35     <!-- 访问静态资源 -->
36     <mvc:resources location="/image" mapping="/**"/>
37     
38 </beans>  

     其中,在原来的基础上添加的配置有:

      a) 22~23行(用于自动注册组件)

      b) 35~36行(用于访问静态资源,按照这个格式也可以添加js,css或其他需要加载的静态资源,网上有别的写法:<mvc:resources location="/image/" mapping="/image/**">也可以正常加载出image文件夹下的图片)。

三. 测试

  3.1. 在WEB-INF下创建名为image的文件夹,将图片拷贝到这个文件夹中,最后项目结构如下图:

  3.2. 启动tomcat,在浏览器地址栏输入url:http://localhost:8080/SpringMVC/index.jsp

    点击"登录"按钮,页面跳转,图片加载成功:

四. 总结

   网上有资料称还有别的两种方法,这里不再赘述。

原文地址:https://www.cnblogs.com/yadongliang/p/6495019.html