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

一. 问题及需求

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

web.xml:html

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

 loginSucc.jsp:java

 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>

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

二. 解决方案

   只须要修改springmvc.xml: spring

 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行(用于自动注册组件)spring-mvc

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

三. 测试

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

 

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

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

四. 总结

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

相关文章
相关标签/搜索