关于Java Web Spring 框架下将静态文件放于WEB-INF下如何访问
首先,WEB-INF是一个安全文件夹,用户直接通过浏览器进行HTTP访问是不能访问到该文件夹下的任何文件。
在Spring框架下页面的展示均是通过ModieAndView来进行展示的,是由框架来访问文件。
但是,在jsp文件里面引用的静态文件如:js,css,img均会因为没通过框架直接通过http来请求文件,会导致不通。
文件构架如下:
由于文件均是通过框架来进行访问的,因此,我们需要在spring框架里面对static文件进行一个映射!
在spring.xml文件夹里面添加内容:
<!-- 静态资源映射 -->
<mvc:resources mapping="/css/**" location="/WEB-INF/static/css/"></mvc:resources>
<mvc:resources mapping="/js/**" location="/WEB-INF/static/js/"></mvc:resources>
<mvc:resources mapping="/img/**" location="/WEB-INF/static/img/"></mvc:resources>
当然在spring.xml文件的头加入标签的引用
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"
JSP
<style type="text/css">@import "css/main.css";</style>
这样在jsp中引用的静态文件就能被访问到了!