使用Spring 3提供静态内容服务

11

我正在尝试使用Spring 3的资源映射功能,但似乎无法正常工作。这是我的代码:

<servlet>
    <servlet-name>aaa</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>aaa</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

在我的web.xml中,

然后在我的aaa-servlet.xml文件中,我有以下内容:

<resources mapping="/resources/**" location="/resources/" />
我访问JSP中的内容如下所示:
<link href="<c:url value="/resources/css/blueprint/screen.css" />" rel="stylesheet" type="text/css">

我阅读了相关的信息,发现我已经正确地设置了一切,但是它却没有起作用。我正在使用Weblogic服务器,在启动时它会映射/resources/文件夹。

任何帮助都将不胜感激!

完整的aaa-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xsi:schemaLocation="
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />



<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/jsps directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/jsps/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<beans:bean id="messageSource"  
    class="org.springframework.context.support.ResourceBundleMessageSource">  
    <beans:property name="basename" value="messages"/>  
</beans:bean>  

<!-- Imports user-defined @Controller beans that process client requests -->
<beans:import resource="controllers.xml" />

controllers.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- Scans within the base package of the application for @Components to configure as beans -->
<context:component-scan base-package="com.app.controller" />

启动日志:

INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/**] onto handler 'org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0'
INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/resources/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'

你尝试过添加 <mvc:annotation-driven/> 吗? - Jack Edmonds
1
@Ralph 我不是完全确定 :P。但我知道我曾经遇到过非常类似的问题,添加 <mvc:annotation-driven/> 神奇地解决了它。更详细的描述在这里:http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html - Jack Edmonds
我已经使用了<mvc:annotation-driven />,但是我收到了404错误 - 未找到。我已经尝试用常规链接替换c:url,但仍然出现相同的错误,如果我尝试直接将链接localhost:7001/XXX/resources/css/.....css放入浏览器中也是如此。 - YBB
YBB:请在您的帖子中添加屏幕.css文件从war包的位置。 - Ralph
resources/css/blueprint/screen.css - YBB
显示剩余2条评论
3个回答

4

您正在将您的应用映射到根上下文,因此您可能需要包含:

<mvc:default-servlet-handler/>

在您的MVC配置文件中,可以参考Spring文档中的15.12.5章节。当我的调度器映射到/时,我无法使mvc:resources正常工作,除非进行了该设置。至少,在我几个月前配置这个项目时,我记得必须这样做。


我实际上已经尝试使用这个了,虽然有这个和将服务器映射到 / 的操作不是多余的吗?无论如何,有这一行都没有任何区别。 - YBB
这并不是多余的。只有在将DispatcherServlet映射到根上下文时才需要它。文档中的第一行说明:“此标记允许将DispatcherServlet映射到“/”(从而覆盖容器默认Servlet的映射),同时仍然允许静态资源请求由容器默认Servlet处理。”如果没有任何“default-servlet-name”,则会使用合理的默认值,具体取决于容器,但如果您有其他默认servlet,则也可以配置它。 - digitaljoel

1

尝试将您的<resources mapping="/resources/**" location="/resources/" />

替换为

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

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> - YBB
如果我查看源代码,CSS链接将生成以下内容: <link href="/XXX/resources/css/blueprint/screen.css" rel="stylesheet" type="text/css"> - YBB
其中XXX是应用程序上下文。 - YBB
@YBB,你的客户端代码看起来很好。最后一次尝试:你能发布你的aaa-context.xml吗? - Stealth
posted in the original entry. - YBB
显示剩余4条评论

0
修好了!
看起来ResourceHttpRequestHandler中有一个只在Weblogic上出现的bug。
修复方法如下:
删除

<resources mapping="/resources/**" location="/resources/"/>

添加

<default-servlet-handler/>

我知道有些人建议使用默认的servlet处理程序,但资源映射必须被删除才能使其正常工作!


2
如果您需要删除资源映射,那么如何将目录映射为静态资源呢? - digitaljoel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接