Thursday, July 19, 2012

Order of Annotated Controllers When Matching @RenderMapping and @ActionMapping

ConfigurationJDK 7, Spring 3.1.1.RELEASE and Hibernate 4.1.3.Final. Multiple annotated controllers.

When you invoke a Render or Action mapping, it might happen the default mapping is picked up instead of the correct one.
This happens when the controller with the default mapping is first in the list.

DEBUG [http-8080-1] (AbstractMapBasedHandlerMapping.java:74) - Key [view] -> handler [{org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping$RenderMappingPredicate@2e19e=my.package.controller.DefaultController@12a8bad, ...

It's probably a bug or I don't understand the purpose yet.
This can be fixed by forcing Spring to put the controller with the default mapping last. First you scan all others and separately the default one:

    <context:component-scan base-package="it.italiangrid.liferay.doclibadmin.controller" use-default-filters="true">
        <context:exclude-filter type="regex" expression="EmiDocumentController"/>
    </context:component-scan>
    
    <context:component-scan base-package="it.italiangrid.liferay.doclibadmin.controller" use-default-filters="true">
        <context:include-filter type="regex" expression="EmiDocumentController"/>
    </context:component-scan>

Update I found out that if you use @RequestMapping instead of @RenderMapping and @ActionMapping in all methods of the controller with the default action, the problem seems to disappear.
You can use render and action mapping annotations in all other controllers.

References

No comments: