简体   繁体   English

使用Spring 3提供静态内容

[英]Serving static content with Spring 3

I'm trying to use resources mapping feature of Spring 3 and it does not seem to be working. 我正在尝试使用Spring 3的资源映射功能,但它似乎没有用。 Here is what I have: 这是我有的:

<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>

in my web.xml 在我的web.xml中

then in my aaa-servlet.xml I have the following: 然后在我的aaa-servlet.xml中我有以下内容:

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

I'm accessing the content in jsp like this: 我正在访问jsp中的内容,如下所示:

<link href="<c:url value="/resources/css/blueprint/screen.css" />" rel="stylesheet" type="text/css">

Everything I've been reading suggests that I have it all setup correctly however it is not working. 我一直在阅读的所有内容都表明我已经正确设置了所有内容,但它无法正常工作。 I'm using weblogic server and on startup it does map the /resources/ folder. 我正在使用weblogic服务器,在启动时它会映射/ resources /文件夹。

Any help would be greately appreciated! 任何帮助都会非常感激!

aaa-servlet.xml in its entirety: 完整的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: 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" />

Startup log: 启动日志:

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'

You are mapping your app to the root context, so you should probably include 您正在将应用程序映射到根上下文,因此您可能应该包括

<mvc:default-servlet-handler/>

in your mvc config. 在你的mvc配置中。 Have a look at 15.12.5 in the spring docs . 春季文档中查看15.12.5。 I wasn't able to get mvc:resources to work without that setting when my dispatcher servlet was mapped to /. 当我的调度程序servlet映射到/时,我无法让mvc:资源在没有该设置的情况下工作。 At least, that's what I seem to remember having to do when I configured this a couple of months ago in my project. 至少,这是我几个月前在我的项目中配置它时我似乎记得要做的事情。

Try replacing your <resources mapping="/resources/**" location="/resources/" /> 尝试替换<resources mapping="/resources/**" location="/resources/" />

with

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

Got it fixed! 修好了!

Looks like there is a bug in ResourceHttpRequestHandler which only appears with Weblogic. 看起来ResourceHttpRequestHandler中存在一个只出现在Weblogic中的错误。

To fix the issue: 要解决此问题:

Remove 去掉

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

Add

<default-servlet-handler/>

I know some of you suggested using the default-servlet-handler but the resources mapping must be removed in order for it to work! 我知道有些人建议使用default-servlet-handler,但必须删除资源映射才能使其正常工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM