我一直在关注使用Spring提供静态文件(CSS,JS等)的几个教程,从我可以看到我应该能够看到我的静态文件,但我不能。
我的spring-web-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: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-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="brass.ducks" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:resources mapping="/styles/**" location="views" />
<mvc:annotation-driven/>
</beans>
最后一行表明styles/whatever.css
后面的任何网址都应该在我的观看文件夹中查找并从那里开始提供。例如,我的页面有一个指向样式表的链接/styles/css/bootstrap.min.css
(可以完成http://localhost:8080/Brass-Ducks/styles/css/bootstrap.min.css
),但这不会解析为我的样式表。
我的文件夹层次结构如下所示:
我错过了什么吗?
修改
根据建议我提取了我的.war
文件,发现文件夹层次结构如下所示:
我已将web.xml
修改为如下所示:
<mvc:resources mapping="/styles/**" location="/WEB-INF/views/" />
<mvc:annotation-driven/>
但网址http://localhost:8080/Brass-Ducks/styles/css/bootstrap.min.css
仍然无法解析CSS文件。
答案 0 :(得分:1)
将styles
目录移至/webapp/resourses
路径。所以你将/webapp/resourses/styles
。使用<mvc:resources mapping="/styles/**" location="/resources/styles/"
更改xml配置,并尝试从http://localhost:8080/Brass-Ducks/styles/css/bootstrap.min.css
答案 1 :(得分:0)
将映射配置更改为:
<mvc:resources mapping="/styles/**" location="/WEB-INF/views/styles/" />
答案 2 :(得分:-1)
是的,你错过了/
,它应该是这样的:
<mvc:resources mapping="/styles/**" location="/views/" />
和XML配置spring并不是一个好主意,试试java config ..