Spring MVC 静态资源访问 mvc:resources 标签

来源:互联网 发布:怎样生成淘宝客链接 编辑:程序博客网 时间:2024/04/29 03:10

最近又开始了 Java Web 之旅 很久没有做了,复习一下 Spring,Spring MVC和Mybatis。


使用的版本是 Spring 4的核心库,在配置静态资源的时候出了一些问题。


我把静态资源整合在了一个 public 文件夹下 然后配置了 


<mvc:resources location="/public/" mapping="/public/**"></mvc:resources>


然而并没有什么卵用,百度了一下,七七八八都说需要加上下面这句话


<mvc:annotation-driven />


加载注解映射器和适配器我已经配置了,这就郁闷了,搞了半天,发现一个很细节的问题,我的资源放在了WEB-INF下,看了下网上的说法,个人观点同意把静态资源

放在WEB-INF下,因为安全。这下问题就很明朗了我在标签下加了一句话


<mvc:resources location="/WEB-INF/public/" mapping="/public/**"></mvc:resources>


最后贴一下 我的配置 springmvc.xml(spring-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"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"><mvc:annotation-driven /><!-- 对静态资源文件的访问 方案一 --><mvc:resources location="/WEB-INF/public/" mapping="/public/**"></mvc:resources><!-- 使用扫描的方法加载Handler --><context:component-scan base-package="com.leo.ssm.controller" /><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 配置前缀和后缀 简化 url路径的设置 --><property name="prefix" value="/WEB-INF/" /><property name="suffix" value=".jsp" /></bean></beans>


web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID" version="3.0"><display-name>ExjModel</display-name><!-- 加载spring容器 --><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- Spring mvc 前端控制器 --><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/springmvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list></web-app>


工程目录结构



0 0
原创粉丝点击