How to map static assets in my spring application?

I am using Spring 3.0.5. I have all my static assets in a folder named "static" in the root of my web application (at the same level as WEB-INF). How do I match the URLs of the form "http://mydomain.com/context-path/static/some-asset" in my "static" folder?

This is complicated by the fact that I have a view definition tool that displays in the root context (from my web.xml) ...

<!-- Declare a Spring MVC DispatcherServlet as usual --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- Configure DispatcherServlet to use AnnotationConfigWebApplicationContext instead of the default XmlWebApplicationContext --> <init-param> <param-name>contextClass</param-name> <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> 

Ok, thanks for any help, - Dave

PS - Adding mvc: resources did not seem to heal the pain. I added parentContext.xml to my file ...

 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns="http://www.springframework.org/schema/beans" 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"> <mvc:resources mapping="/static/**" location="/static/"/> 

but then received exceptions: "SEVERE: Servlet.service () for the servlet manager threw javax.servlet.ServletException: no adapter for handler [ com.myco.systems.leadsmonitor.web.controller.HomeController@6870 c52d]: did your handler execute a supported interface such as a controller?" when I visited my "/" homepage.

+4
source share
2 answers

In addition to using <mvc:resources /> make sure you have this line in the same file:

 <mvc:annotation-driven /> 

I don’t know why this happens, but when you use <mvc:resources /> , it disables some really important part of the MVC mechanism - respecting your comments on the Controller and RequestMapping, which I assume. I believe that <mvc:annotation-driven /> tells your computer that you really need it (?).

Good luck. I just struggled with the same problem. Here's what my last looks like:

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- Explicitly enables the Spring MVC @Controller programming model --> <mvc:annotation-driven /> <mvc:resources mapping="/static/**" location="/static/" /> <context:component-scan base-package="com.seemikecode.controller" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans> 
+4
source

A better option is to use <mvc:resources />

+4
source

All Articles