2015-11-06 200 views
0

我想在我的示例spring mvc項目中使用靜態資源,在我的項目中,我在資源文件夾下創建了一個主題,如圖所示。 enter image description herespring mvc靜態資源加載失敗

然後我就加入彈簧MVC-servlet.xml中以下3行代碼

<?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:p="http://www.springframework.org/schema/p" 
    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-4.1.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-4.1.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> 

    <context:component-scan base-package="com.sudheer.example" /> 

    <context:annotation-config /> 

    <bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 

     <property name="prefix"> 
      <value>/WEB-INF/jsp/</value> 
     </property> 

     <property name="suffix"> 
      <value>.jsp</value> 
     </property>   

    </bean> 

    <mvc:resources mapping="/resources/**" location="/resources/theme/" cache-period="31556926"/> 
    <mvc:default-servlet-handler/> 
    <mvc:annotation-driven /> 

</beans> 

WhenI嘗試訪問靜態資源在許多不同的充方式,我不能夠訪問它。 這是我的jsp頁面:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> 
<!DOCTYPE html> 
<html lang="en"> 
<head> 
<link href="/resources/themes/css/bootstrap.min.css" rel="stylesheet" > 

<link rel="stylesheet" type="text/css" 
href="${pageContext.request.contextPath}/themes/bootstrap.min.css"> 
<link href="<c:url value="/resources/themes/css/bootstrap.min.css"/>" 
rel="stylesheet" type="text/css"/> 

</head> 
<body> 
    <h1>1. Test CSS</h1> 
    <h2>2. Test JS</h2> 
    <div class="btn"></div> 
</body> 
</html> 

,這是我的web.xml

<!DOCTYPE web-app PUBLIC 
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
"http://java.sun.com/dtd/web-app_2_3.dtd" > 

<web-app> 
    <display-name>Archetype Created Web Application</display-name> 
    <servlet> 
     <servlet-name>spring-mvc</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring-mvc-servlet.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>spring-mvc</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 

任何一個可以幫我解決這個問題呢?

回答

1

大概是這樣的:

您有這方面的配置

<mvc:resources mapping="/resources/**" location="/resources/theme/" 

但是,當你調用resourcek,你使用這個

<c:url value="/resources/themes/css/bootstrap.min.css"/ 

我htink你必須使用這個

/resources/css/bootstrap.min.css 
1

你有3個問題:

  • 1:在你的Maven項目,文件夾src/main/resources有一個Maven的特殊意義:它的內容將被放置在classpath中編譯後。
  • 2:當您使用彈簧資源映射時,映射匹配映射部分'**'的url部分將被添加到獲取文件的位置部分。 (抱歉不好解釋)一個例子更清楚:假設你有這樣的資源映射

    <mvc:resources mapping="https://stackoverflow.com/a/**" location="/b/c/" > 
    

    你請求URL http://localhost/myApp/a/d Spring會查找文件在/b/c/d! - 所以,當你發送一個請求....../resources/themes/css/bootstrap.min.css春季將搜索:/resources/theme/themes/css/bootstrap.min.css

  • 3:在春天的配置和文件夾使用theme但在JSP使用themes


解決方案1將會是:要麼更改location彈簧資源映射以通過在位置屬性中使用classpath:前綴從類路徑中選取資源,要麼將文件放在其他文件夾中:source/main/webapp/resources/theme(然後位置參數/resources/theme/)將映射。

與類路徑修復例如用於和一個可能的修復爲:

<mvc:resources mapping="/resources/theme/**" 
    location="classpath:/resources/theme/" cache-period="31556926"/> 

JSP - 固定爲:

<link href="<c:url value="/resources/theme/css/bootstrap.min.css"/>" 
     rel="stylesheet" type="text/css"/> 
1

如果資源由一個需要可見前端,你需要把資源放在「webapp」文件夾中。例如:webapp/theme/css/bootstrap.min.css

1

我已經爲Spring MVC創建了一個框架項目(也使用了靜態資源)here