views:

322

answers:

1

So I'm trying to configure my web app without XML and go the all annotated route. I have a few classes annotated with @Configuration and @Component that are getting picked up automatically, but for some reason my @Controller annotations aren't being recognized and mapped to their corresponding @RequestMapping values.

My web.xml file looks like so:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="com-timbuk2-webapp-compositor" 
         version="3.0"
         xmlns="http://java.sun.com/xml/ns/javaee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" >

    <display-name>timbuk2-webapp-Compositor</display-name>

    <!-- Context Parameters -->
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/conf/log4j-config.xml</param-value>
    </context-param>

    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>

    <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>com.company.webapp</param-value>
    </context-param>

    <!-- Listeners -->
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Filters -->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter>
        <filter-name>urlRewriteFilter</filter-name>
        <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
        <init-param>
            <param-name>logLevel</param-name>
            <param-value>commons</param-value>
        </init-param>
        <init-param>
            <param-name>confPath</param-name>
            <param-value>/WEB-INF/conf/urlrewrite-config.xml</param-value>
        </init-param>
    </filter>

    <!-- Filter Mappings -->
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>urlRewriteFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- Servlets -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>

    <!-- Servlet mappings -->
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/app/*</url-pattern>
    </servlet-mapping>


</web-app>

My main @Configuration class looks like so:

package com.company.webapp.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
public class ApplicationConfiguration
{    
    @Bean
    public ReloadableResourceBundleMessageSource messageSource()
    {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename( "/WEB-INF/resources/messages" );
        messageSource.setCacheSeconds( 0 );
        return messageSource;
    }

    @Bean
    public InternalResourceViewResolver viewResolver()
    {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass( JstlView.class );
        viewResolver.setPrefix( "/WEB-INF/views/" );
        viewResolver.setSuffix( ".jsp" );
        return viewResolver;
    }
}

My @Controller and @Component classes all live under the same package. For instance, here's a @Controller class from my app:

package com.company.webapp.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping( value = "/render" )
public class RenderController
{
    private final Logger logger = LoggerFactory.getLogger( getClass() );

    @RequestMapping( method = RequestMethod.GET )
    public ModelAndView handleGet( BindingResult bindingResult )
    {
        ... eventually return a ModelAndView object...
    }
}

So to reiterate, my @Controller classes aren't being mapped to the URLs I'm specifying. When the app starts up or reloads in my local tomcat environment I'm not seeing the usual "Mapped URL "/render" to..." console output.

Can anyone tell me what I'm missing that might prevent my @Controller annotated classes not to be found and registered?

+1  A: 

I guess this happens because @Controllers should be picked by DispatcherServlet from its own application context (which is loaded from <servletname>-servlet.xml by default), when your contextClass and contextConfigLocation are applied to the root context (the ContextLoaderListener's one, which is loaded from applicationContext.xml by default).

To configure DispatcherServlets application context, you should set contextClass and contextConfigLocation as the servlet's init-params.

EDIT: This behaviour is controlled by the property detectHandlersInAncestorContexts of DefaultAnnotationHandlerMapping, so the alternative approach is to set it to true during configuration:

@Bean
public DefaultAnnotationHandlerMapping mapping() {
    DefaultAnnotationHandlerMapping m = new DefaultAnnotationHandlerMapping();
    m.setDetectHandlersInAncestorContexts(true);
    return m;
}
axtavt
I set the contextClass and contextConfigLocation parameters of the dispatcher servlet to the same values as the webapp. All seems well, I guess now I'm a little confused though. Is there two contexts running now? Before, when I was doing all XML configuration, the URL mappings would register when I would start or reload the web app. Now they don't get mapped until the first incoming request is made.
Matt W
1) Yes, you have two application context - the root context and the DispatcherServlet`s context, which is a child of the root one. This is typical for Spring MVC applications, see http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-servlet. However, it's not a good idea to make them load the annotation-based configuration from the same package - beans would be duplicated. 2) DispatcherServlet's context is initialized when servlet is loaded, so you can control in with `<load-on-startup>`. 3) I added the alternative solution.
axtavt
Ahhhh...I think I finally understand now. Thanks a lot.
Matt W