2015-04-08 63 views
1

這是我的dispacter-servlet.xml文件。當我部署在樹脂親項目4.0.36加載我的索引頁面和內容,但無法加載staticresources文件夾下存儲的CSS文件和圖像調度器servlet映射資源(Resin Server)

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans-3.0.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="com.dogears" /> 


<bean id="viewResolver" 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix"> 
     <value>/WEB-INF/pages/</value> 
    </property> 
    <property name="suffix"> 
     <value>.jsp</value> 
    </property> 
</bean> 

<import resource="classpath:beans.xml"/> 

<mvc:resources mapping="/resources/**" location="/staticresources/" /> 
<mvc:annotation-driven /> 

請誰能告訴我如何映射我的靜態資源文件夾,以便無論何時請求是patter/resources/*,它都會將請求重定向到靜態資源文件夾。 staticresources文件夾位於MyspringProject/src/main/webapps目錄

+0

http://stackoverflow.com/questions/26325524/match-for-root-url-and-serving-of-static-resources – OO7

+0

試試我的答案。願它幫助你。 – OO7

回答

0

我終於找到了我的解決方案。我已將我的spring webapp部署到resin目錄的webapps文件夾中。

我意識到<mvc:resource />映射標籤的確,當你已經部署樹脂服務器,而不是tomcat的你的春天應用程序不工作。

因此,我解決了這個首先創建我的項目的war文件,然後提取桌面上的戰爭文件中,隨後放入所有內容從war文件中的的webapps /根(而不是web應用文件夾)的文件夾的樹脂目錄。然後從我的索引頁面使用JSTL TAG來包含外部樣式表。

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<html> 
<head> 

<link rel="stylesheet" type="text/css" href="<c:url value="/staticresources/externalcss.css"/>"> 

</head> 
<body> 
<h2 class="text_style">Hello World!</h2> 
</body> 
</html> 

IT WORKS!

+0

感謝球員的所有幫助! – user2522497

0

假設你項目目錄結構就像

- src 
    - main 
     - webapp 
     - resources 
      - css 
      - abc.css 
      - images 
      - xyz.jpg 

&您的.html個或.jsp頁面所在的目錄,如下

- webapp 
    - index.jsp 
    - pages 
    - welcome.jsp 

那麼你可以index.jsp頁面的URL BaseURL/index.jsp

<link href="resources/css/abc.css" rel="stylesheet" type="text/css" /> 

<body> 
    <img src="resources/images/xyz.jpg" alt="" style="width:100px;height:100px" /> 
</body> 

&添加鏈接到你的資源welcome.jsp與網址BaseURL/pages/welcome.jsp as

<link href="../resources/css/abc.css" rel="stylesheet" type="text/css" /> 

<body> 
    <img src="../resources/images/xyz.jpg" alt="" style="width:100px;height:100px" /> 
</body> 

希望這對你有所幫助。

+0

只能識別mvc:資源映射。所以當我調用資源時,它並沒有引用靜態資源文件夾。 – user2522497

+0

我正在做一個spring + maven項目 – user2522497

+0

我沒有明確表達你的意思嗎?你能再詳細一點嗎? – OO7