Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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'
like image 445
YBB Avatar asked Jul 27 '11 15:07

YBB


People also ask

How does Spring Boot serve static content?

Using Spring BootSpring Boot comes with a pre-configured implementation of ResourceHttpRequestHandler to facilitate serving static resources. By default, this handler serves static content from any of the /static, /public, /resources, and /META-INF/resources directories that are on the classpath.

What is static content serving?

Definition. Static content is any content that can be delivered to an end user without having to be generated, modified, or processed. The server delivers the same file to each user, making static content one of the simplest and most efficient content types to transmit over the Internet.


2 Answers

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.

like image 71
digitaljoel Avatar answered Oct 24 '22 11:10

digitaljoel


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

with

<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
like image 33
Stealth Avatar answered Oct 24 '22 10:10

Stealth