11

I'm trying to use resources mapping feature of Spring 3 and it does not seem to be working. 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

then in my aaa-servlet.xml I have the following:

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

I'm accessing the content in jsp like this:

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

Any help would be greately appreciated!

aaa-servlet.xml in its entirety:

<?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" />

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'
YBB
  • 111
  • 1
  • 5
  • Have you tried adding ``? – Jack Edmonds Jul 27 '11 at 15:43
  • Have you tried just using a normal link without the c:url ? – Tomas F Jul 27 '11 at 15:50
  • @Jack: what should be the purpose of mvc:annotation-driven in this scenario? – Ralph Jul 27 '11 at 15:51
  • 1
    @Ralph I'm not completely sure :P. But I know I was having a very similar problem and adding `` magically solved it. It's described in more detail here: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html – Jack Edmonds Jul 27 '11 at 16:31
  • I already use I get Error 404--Not Found I already tried replacing c:url with regular link Same error if I try to put the link localhost:7001/XXX/resources/css/.....css directly into the browser – YBB Jul 27 '11 at 16:58
  • YBB: please add the location of screen.css form the war to your post. – Ralph Jul 27 '11 at 18:36
  • resources/css/blueprint/screen.css – YBB Jul 27 '11 at 19:50

3 Answers3

4

You are mapping your app to the root context, so you should probably include

<mvc:default-servlet-handler/>

in your mvc config. Have a look at 15.12.5 in the spring docs. I wasn't able to get mvc:resources to work without that setting when my dispatcher servlet was mapped to /. At least, that's what I seem to remember having to do when I configured this a couple of months ago in my project.

digitaljoel
  • 26,265
  • 15
  • 89
  • 115
  • I actually already tried using this one although isn't it redundant to have this and do the server mapping to / ? Either way having this line makes no difference. – YBB Jul 28 '11 at 14:25
  • it's not redundant at all. It's only needed if you map the dispatcherservlet to the root context. The first line in the docs states "This tag allows for mapping the DispatcherServlet to "/" (thus overriding the mapping of the container's default Servlet), while still allowing static resource requests to be handled by the container's default Servlet." Using it without any "default-servlet-name" will use a reasonable default depending on the container, but if you have something else for the default servlet you can also configure that. – digitaljoel Jul 29 '11 at 04:06
1

Try replacing your <resources mapping="/resources/**" location="/resources/" />

with

<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
Stealth
  • 1,559
  • 4
  • 16
  • 34
  • I already use I get Error 404--Not Found I already tried replacing c:url with regular link Same error if I try to put the link http://localhost:7001/XXX/resources/css/.....css directly into the browser – YBB Jul 27 '11 at 16:52
  • `<%@ 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" %> ` – YBB Jul 27 '11 at 18:05
  • ` ${title} " rel="stylesheet" type="text/css"> ` – YBB Jul 27 '11 at 18:05
  • And if I view source, the css link produces the following: `` – YBB Jul 27 '11 at 18:07
  • Where XXX is the application context. – YBB Jul 27 '11 at 18:13
  • @YBB Your client side code looks fine. Final try: can you post your aaa-context.xml? – Stealth Jul 27 '11 at 19:37
  • posted in the original entry. – YBB Jul 27 '11 at 19:59
  • @YBB I have an existing Spring MVC application with almost exact settings as you have here. The only thing different that I have is both my and have mvc: in the beginning like and – Stealth Jul 27 '11 at 21:26
  • Including mvc: makes no difference. – YBB Jul 28 '11 at 14:28
0

Got it fixed!

Looks like there is a bug in ResourceHttpRequestHandler which only appears with 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!

YBB
  • 111
  • 1
  • 5