SpringMVC 的<mvc:resources>使用映射路径展示文件服务器上的图片

本文介绍如何在Spring MVC中配置静态资源的访问,包括使用&lt;mvc:resources&gt;标签、&lt;mvc:default-servlet-handler&gt;等方法解决静态资源访问问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

接上一篇提到的部署中的问题,可以采用 <mvc:resources>即可解决,然而使用这种方式也不是一帆风顺的,需要解决其他的一些配置问题。

首先,spring mvc 的<mvc:resources mapping="***" location="***">标签是在spring3.0.4出现的,主要是用来进行静态资源的访问。我们项目中是这样使用的:

<mvc:resources mapping="/${mapping_path}/**" location="file:${local_path}/">

对应的xx.propterties文件中的值如下:

mapping_path=poster

local_path=D:/poster

但是发现这样,在<img src="http://localhost:7001/pad-sces/poster/xxx.jpg"/>下并没有成功展示图片,百思不得其解。后来发现原来这个还与org.springframework.web.servlet.DispatcherServlet的中的url-pattern配置有关,期初在web.xml中我们项目的配置如下:

	<servlet>
		<servlet-name>springMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param> 
  		<param-name>contextConfigLocation</param-name> 
  		<param-value>classpath:/spring/spring-mvc-servlet.xml</param-value> 
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
这样是没有效果的,*.do是拦截了所有*.do的请求到servlet中,后来改成
<url-pattern>/</url-pattern>

这样就可以成功展示了,但是问题又出现了,就是页面上引用的css,js等效果不起作用了,报错如下:

WARNING: No mapping found for HTTP request with URI [/pad-sces/js/login.js] in DispatcherServlet with name 'springMVC'

原因是:使用 / 拦截了所有的请求,会影响到静态资源文件的获取,这样就需要用mvc:default-servlet-handler和mvc:resources来帮住分类完成获取静态资源的责任。在spring-mvc-servlet.xml中配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="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">   

	<mvc:default-servlet-handler />
	<mvc:resources mapping="/${mapping_path}/**" location="file:${local_path}/">
	<mvc:resources mapping="/js/**" location="/js/"/>
	<mvc:resources mapping="/css/**" location="/css/"/>
	<mvc:resources mapping="/images/**" location="/images/"/>
	<mvc:resources mapping="/plugin/**" location="/plugin/"/>
</beans>

这样就可以成功展示了图片了,也能正常显示其他效果。

注意:引用上述js、css等效果的page页面目录不需要映射。location是工程路径地址,mapping是映射后的访问地址,建议js、css等效果的映射地址和工程路径地址一致,这样访问起来比较容易。

此外还要注意,file:是针对非项目工程中的文件访问,本项目工程中的文件路径不要加file:

============================================================================

当然,web.xml的DispatcherServlet还可以配置多个url-pattern,如上面所述的,我们可以改成:

<servlet>
		<servlet-name>springMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param> 
  		<param-name>contextConfigLocation</param-name> 
  		<param-value>classpath:/spring/spring-mvc-servlet.xml</param-value> 
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<url-pattern>*.do</url-pattern>
		<url-pattern>/poster/*</url-pattern>
	</servlet-mapping>
这样在spring-mvc-servlet.xml可以直接这样配置:

<mvc:resources mapping="/**" location="file:${local_path}/">

不过,有个不好之处在于web.xml必须是写死的url-pattern值/poster/* 而不能像上述方法那样配置参数值,可配置性不高。

==============================================================================

以下内容是分享网友的:

如果你的DispatcherServlet拦截 *.do这样的URL,就不存在访问不到静态资源的问题。如果你的DispatcherServlet拦截“/”,拦截了所有的请求,同时对*.js,*.jpg的访问也就被拦截了。

目的:可以正常访问静态文件,不要找不到静态文件报404

方案一:激活TomcatdefaultServlet来处理静态文件

<span style="font-size:12px;"> <servlet-mapping>
 	<servlet-name>default</servlet-name>
 	<url-pattern>*.jpg</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 	<servlet-name>default</servlet-name>
 	<url-pattern>*.js</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 	<servlet-name>default</servlet-name>
 	<url-pattern>*.css</url-pattern>
 </servlet-mapping></span>

要写在DispatcherServlet的前面, 让 defaultServlet先拦截,这个就不会进入Spring了。

方案二: spring3.0.4以后版本提供了mvc:resources 的使用方法:

<span style="font-size:12px;"> <!-- 对静态资源文件的访问 -->
 <mvc:resources mapping="/images/**" location="/images/" /></span>

/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/>元素,把mappingURI注册到SimpleUrlHandlerMappingurlMap中,keymappingURI pattern值,而valueResourceHttpRequestHandler,这样就巧妙的把对静态资源的访问由HandlerMapping转到ResourceHttpRequestHandler处理并返回,所以就支持classpath目录,jar包内静态资源的访问.另外需要注意的一点是,不要对SimpleUrlHandlerMapping设置defaultHandler。因为对static uridefaultHandler就是ResourceHttpRequestHandler,否则无法处理static resources request

方案三 ,使用<mvc:default-servlet-handler/>

<span style="font-size:12px;"> <mvc:default-servlet-handler/></span>

会把"/**" url,注册到SimpleUrlHandlerMappingurlMap,把对静态资源的访问由HandlerMapping转到org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler处理并返回DefaultServletHttpRequestHandler使用,就是各个Servlet容器自己的默认Servlet

补充说明:多个HandlerMapping的执行顺序问题:

DefaultAnnotationHandlerMappingorder属性值是:0

<mvc:resources/ >自动注册的SimpleUrlHandlerMappingorder属性值是:2147483646

<mvc:default-servlet-handler/>自动注册的SimpleUrlHandlerMappingorder属性值是:2147483647

spring会先执行order值比较小的。当访问一个a.jpg图片文件时,先通过DefaultAnnotationHandlerMapping来找处理器,一定是找不到的,我们没有叫a.jpgAction。再按order值升序找,由于最后一个SimpleUrlHandlerMapping是匹配"/**"的,所以一定会匹配上,再响应图片。

注:如果DispatcherServlet拦截 *.do这样的URL,就不存上述问题了。










实验十二 SpringMVC 一、实验目的 1、掌握SpringMVC项目构建 2、掌握SpringMVC数据接收与传递的原理 二、实验过程 1、配置SpringMVC开发环境 a)创建新的项目,TestSpringMvc b)为项目添加Web框架和SpringMVC框架 c)添加classes目录 d)添加javaee依赖 e)添加lib输出配置 将依赖文件添加至lib中 f)修改web.xml配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.form</url-pattern> </servlet-mapping> <filter> <filter-name>charactorEncoding</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>charactorEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> g)修改applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven /> <mvc:default-servlet-handler /> <context:annotation-config></context:annotation-config> <context:component-scan base-package="cs18.controller"/> </beans> h)添加controller,TestController.java @Controller public class TestController { @RequestMapping(value = "/showName") public String showName(HttpServletRequest request, HttpServletResponse response) { //访问传递的参数 ; return "index.jsp"; } } i)配置tomcat,添加项目部署 j)启动tomcat,在浏览器中输入以下地址 http://localhost:8080/TestSpringMvc/showName?name=fredy 2、测试以下不同类型数据绑定方法 a)HttpServletRequet,HttpSession,Model/ModelMap b)简单数据类型绑定(int,String等) c)POJO数据绑定 d)包装POJO数据绑定 e)自定义数据类型绑定 f)数组或集合绑定 g)使用Json风格类型的数据绑定
最新发布
06-11
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值