2016-09-26 167 views
0

我一直在關注使用Spring服務靜態文件(CSS,JS等)的幾個教程,從我可以看到我應該能夠看到我的靜態文件,但我不能。使用Spring服務靜態內容

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任何URL應該看看我的視圖文件夾,並從那裏服務。例如,我的頁面有一個指向樣式表的鏈接/styles/css/bootstrap.min.css(它將完成到http://localhost:8080/Brass-Ducks/styles/css/bootstrap.min.css),但這不會解析爲我的樣式表。

我的文件夾結構如下所示:

enter image description here

我這麼想嗎?

編輯

至於建議我抽出我的.war文件,發現文件夾層次結構是這樣的:

enter image description here

我修改我的web.xml看起來像這樣:

<mvc:resources mapping="/styles/**" location="/WEB-INF/views/" /> 
<mvc:annotation-driven/> 

但是URL http://localhost:8080/Brass-Ducks/styles/css/bootstrap.min.css st生病不解決CSS文件。

回答

-1

是的,你缺少的/,它應該是這樣的:

<mvc:resources mapping="/styles/**" location="/views/" /> 

和XML是不是真的配置Spring,嘗試的Java配置一個好主意..

+0

通過'http://本地主機:8080 /黃銅鴨子/風格/ CSS/bootstrap.min.css'仍然不顯示樣式表。 – Nanor

+0

@Nanor用文件瀏覽器打開你的'war'文件,檢查它是否存在。那裏的迴應是'404'? – Jaiwo99

+0

我可以在我的文件瀏覽器中看到我的'.war'文件,但我不遵循你的意思。 – Nanor

1

移動你的styles目錄到/webapp/resourses路徑。所以你將有/webapp/resourses/styles。更改的XML配置與<mvc:resources mapping="/styles/**" location="/resources/styles/",並嘗試從http://localhost:8080/Brass-Ducks/styles/css/bootstrap.min.css

0

讓你映射的配置更改爲:

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