How to use the default servlet handler

I want to configure Spring MVC to serve dynamic files mixed with static files, for example (URL => File):

/iAmDynamic.html => /WEB-INF/views/iAmDynamic.html.ftl /iAmAlsoDynamic.js => /WEB-INF/views/iAmAlsoDynamic.js.ftl /iAmStatiHtml => /iAmStatic.html 

DispatchServlet maps to / , annotation-based MVC configuration is enabled, and I have a view controller like this (simplified):

 @Controller public class ViewController { @RequestMapping("*.html") public String handleHtml(final HttpServletRequest request) { return request.getServletPath(); } @RequestMapping("*.js") public String handleJavaScript(final HttpServletRequest request) { return request.getServletPath(); } } 

Spring configuration is as follows:

 <context:component-scan base-package="myPackage" /> <mvc:default-servlet-handler /> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/views/" /> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="cache" value="true" /> <property name="prefix" value="" /> <property name="suffix" value=".ftl" /> </bean> 

Unfortunately this will not work. When this <mvc:default-servlet-handler /> active, I can only access the iAmStatic.html file. When I turn off the default servlet handler, then only the dynamic stuff works. But I want both at once and what should this default handler-servlet-handler do or not? Where is the mistake here?

+7
source share
3 answers

I had a similar problem, none of the requests matched with Spring controllers: I found that I was missing from Spring's config xml:

 <mvc:annotation-driven/> 

It seems to be necessary. From the documentation, this is done as follows:

 Configures the annotation-driven Spring MVC Controller programming model 

I will also enable DefaultServlet to handle static content requests.

So your Spring configurator should look like this:

 <context:component-scan base-package="myPackage" /> <!-- Define location and mapping of static content --> <mvc:resources location="/static/" mapping="/static/**"/> <mvc:default-servlet-handler /> <mvc:annotation-driven/> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/views/" /> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="cache" value="true" /> <property name="prefix" value="" /> <property name="suffix" value=".ftl" /> </bean> 

Hope this helps!

+4
source

You need to define two important configurations.

 <mvc:annotation-driven/> <mvc:default-servlet-handler /> 

<mvc:annotation-driven/> will include the default beans infrastructure, where <mvc:default-servlet-handler /> will configure the handler to serve static resources by redirecting to the default Servlet container.

Also do not forget the namespace mvc ie xmlns:mvc="http://www.springframework.org/schema/mvc"

My full configuration file (using TilesViewResolver) looks below

 <?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven/> <!-- Configures a handler for serving static resources by forwarding to the Servlet container default Servlet. --> <mvc:default-servlet-handler /> <mvc:view-controller path="/" view-name="welcome"/> <mvc:view-controller path="/home" view-name="welcome"/> <bean class="org.springframework.web.servlet.view.tiles3.TilesConfigurer"> <property name="definitions"> <list> <value>/WEB-INF/tiles.xml</value> </list> </property> </bean> <bean class="org.springframework.web.servlet.view.tiles3.TilesViewResolver"> <property name="order" value="1"/> </bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="order" value="2"/> <property name="prefix" value="/WEB-INF/"/> <property name="suffix" value=".jsp"/> </bean> </beans> 

Also, if you have several HandlerMapping considering their order. For those for which you are not explicitly providing order, Spring considers it with the lowest priority.

+1
source

I think the name of the view you are returning from the ViewController is not valid. I expect request.getServletPath() return an empty string for all URLs, because the path to your servlet is probably / , and the Java documentation says getServletPath() returns an empty string for this path. Therefore, the FreeMarker view recognizer probably ignores the view name because it does not know what to show.

However, using a controller class with @RequestMapping is probably not the ideal way to accomplish this. Spring includes a ContentNegotiatingViewResolver , which automatically determines the correct presentation based on the type of content. This overview of ContentNegotiatingViewResolver explains how to configure it.

0
source

All Articles