2015-04-08 66 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