How do I map static assets in my Spring app?
I'm using Spring 3.0.5. I have all my static assets in a folder named "static" at the root of my web-app (at the same level as WEB-INF). How do I map URLs of the form "http://mydomain.com/context-path/static/some-asset" to my "static" folder?
This is complicated by the fact that I have a view resolver that maps to 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>
<p开发者_如何转开发aram-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 didn't seem to heal the pain. I added to my parentContext.xml 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 got the exceptions, "SEVERE: Servlet.service() for servlet dispatcher threw exception javax.servlet.ServletException: No adapter for handler [com.myco.systems.leadsmonitor.web.controller.HomeController@6870c52d]: Does your handler implement a supported interface like Controller?" when I visited my home page "/".
In addition to using <mvc:resources />
, make sure you have this line in the same file:
<mvc:annotation-driven />
I don't know why it happens, but when you use <mvc:resources />
, it disables some really important part of the MVC mechanism -- respecting your Controller and RequestMapping annotations I believe. I guess <mvc:annotation-driven />
tells your computer that you really, really need it (?).
Good luck. I just struggled through the same problem. Here's what my final 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>
The best option is to use <mvc:resources />
精彩评论