Spring MVC-Resources

Spring mvc项目中的web.xml配置的DispatcherServlet对应的url-pattern为"/",即所有的URL请求都会经过Spring MVC的处理。但项目中往往会有许多静态文件,比如:图片文件,css样式文件,文本文件等。

我们没有必要对这些静态文件的访问都设置对应的URL,那样会造成大量重复性的劳动,以及维护上的复杂性。Spring MVC提供了一种机制,可以映射一种URL和一个location,此URL后面接的静态文件,对应着location目录下对应的静态文件。此配置为:

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory -->
	<resources mapping="/resources/**" location="/resources/" />

注意:处理HTTP GET请求/resources/ * *通过有效地服务于静态资源在$ { webappRoot } /resources/目录中;

resources/ ** 两个星号代表resources下的所有子包。

servlet-context.xml:

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory -->
	<resources mapping="/resources/**" location="/resources/" />
	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>
<!-- Imports user-defined @Controller beans that process client requests -->
	<beans:import resource="controllers.xml" />
	<task:annotation-driven />

resources目录结构:

访问这些静态文件:http://localhost:9090/spring-mvc-showcase/resources/form.css

jsp页面效果图:

 源码地址:https://github.com/spring-projects/spring-mvc-showcase.git

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值