Reputation: 329
I am creating a simple application on Spring MVC and tiles integration. Initially I was facing problems in loading the static content pages (like img). So in order to serve the static pages, I included below line in the dispatcher-servlet.xml
<mvc:resources mapping="/resources/**" location="/resources/" />
Now application is able to load the static pages. But somehow, the other requests were not able to reach the controller. On debugging, I found that "RequestMappingHandlerMapping" and "RequestMappingHandlerAdapter" was missing in the handlerMappings and handlerAdapters respectively. When I omit the resources mapping entry from the dispatcher-servlet.xml then my dynamic requests are getting served successfully. As a workaround, I have manually registered these two beans in the xml file.
dispatcher-servlet.xml
<context:annotation-config />
<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
<beans:bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
</beans:bean>
<beans:bean id="handlerAdapter"
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></beans:bean>
<context:component-scan
base-package="com.mds.presentation.controller"></context:component-scan>
<bean
class="org.springframework.web.servlet.view.UrlBasedViewResolver"
id="tilesViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.tiles3.TilesView" />
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
TestController
@Controller
public class TestController {
@RequestMapping(value="/login", method=RequestMethod.GET)
public ModelAndView login(HttpServletRequest req, HttpServletResponse resp) {
ModelAndView mav=new ModelAndView("login");
mav.addObject("home","home");
mav.addObject("buy","buy");
mav.addObject("cart","cart");
return mav;
}
}
My curiosity is why spring is not able to register the appropriate handlers and adapters on its own when including mvc:resources. Is there something I am missing ? I tried searching it in the spring docs and google however couldn't find anything relevant.
Upvotes: 0
Views: 4557
Reputation: 5354
I don't think it makes sense to define those beans on your own.
Do you have <mvc:annotation-driven />
in your XML configs ?
If not, try to add it to your XML file and remove:
<beans:bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
</beans:bean>
<beans:bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
</beans:bean>
This should help for your requests to reach the controller.
Basically, mvc:annotation-driven
tag sets you your Spring context to allow for dispatching requests to Controllers.
The tag will configure two beans DefaultAnnotationHandlerMapping
and AnnotationMethodHandlerAdapter
. Or maybe there are some new beans in a new version of Spring, but anyway that tag will configure them for you.
Upvotes: 1