Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC 3.1.0 M2 - WebApplicationInitializer code-based approach problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC 3.1.0 M2 - WebApplicationInitializer code-based approach problem

    Today I created new application based on Spring MVC 3.1.0 M2. I would like to bootstrap the application with no web.xml and with code-based approach only. But I encountered a problem. My @Controller is never executed.

    Initializer:

    Code:
    public class Initializer implements WebApplicationInitializer {
    
    	public void onStartup(ServletContext servletContext)
    			throws ServletException {
    		AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
    		mvcContext.register(MvcConfig.class);
    		mvcContext.refresh();
    
    		// Register and map the dispatcher servlet
    		ServletRegistration.Dynamic dispatcher = servletContext.addServlet(
    				"dispatcher", new DispatcherServlet(mvcContext));
    		dispatcher.setLoadOnStartup(1);
    		dispatcher.addMapping("/app");
    	}
    }
    Basic MVC configuration

    Code:
    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages = "pl.codeleak.springmvc31demo.web")
    public class MvcConfig {
    	
    	private static final Logger LOG = LoggerFactory.getLogger(MvcConfig.class);
    	
    	@Bean
    	public InternalResourceViewResolver configureInternalResourceViewResolver() {
    		LOG.info("configuring {}", InternalResourceViewResolver.class.getName());
    		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    		resolver.setPrefix("/WEB-INF/views/");
    		resolver.setSuffix(".jsp");
    		return resolver;
    	}
    }
    @Controller

    Code:
    @Controller
    public class HomeController {
    
    	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
    
    	/**
    	 * Simply selects the home view to render by returning its name.
    	 */
    	@RequestMapping(value="/", method=RequestMethod.GET)
    	public String home() {
    		logger.info("Welcome home!");
    		return "home";
    	}
    	
    }
    When application starts (Tomcat 7.0.8) I see following log entries:

    Code:
    INFO : org.springframework.web.SpringServletContainerInitializer - Delegating ServletContext to the following WebApplicationInitializer instances: [pl.codeleak.springmvc31demo.config.Initializer@1b4cd65]
    INFO : org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Thu Jun 16 00:17:39 CEST 2011]; root of context hierarchy
    INFO : org.springframework.context.annotation.ClassPathBeanDefinitionScanner - JSR-330 'javax.inject.Named' annotation found and supported for component scanning
    INFO : org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Registering annotated classes: [class pl.codeleak.springmvc31demo.config.MvcConfig]
    INFO : org.springframework.context.annotation.ClassPathBeanDefinitionScanner - JSR-330 'javax.inject.Named' annotation found and supported for component scanning
    INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
    INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1603bdc: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,mvcConfig,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0,homeController,org.springframework.web.servlet.config.annotation.WebMvcConfiguration#0,requestMappingHandlerMapping,viewControllerHandlerMapping,beanNameHandlerMapping,resourceHandlerMapping,defaultServletHandlerMapping,requestMappingHandlerAdapter,webMvcConversionService,webMvcValidator,httpRequestHandlerAdapter,simpleControllerHandlerAdapter,handlerExceptionResolver,configureInternalResourceViewResolver]; root of factory hierarchy
    INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[]}" onto public java.lang.String pl.codeleak.springmvc31demo.web.controller.HomeController.home()
    WARN : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Neither 'urlMap' nor 'mappings' set on SimpleUrlHandlerMapping
    WARN : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Neither 'urlMap' nor 'mappings' set on SimpleUrlHandlerMapping
    WARN : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Neither 'urlMap' nor 'mappings' set on SimpleUrlHandlerMapping
    INFO : pl.codeleak.springmvc31demo.config.MvcConfig - configuring org.springframework.web.servlet.view.InternalResourceViewResolver
    @Controller seems to be detected properly (component scan). But warnings on org.springframework.web.servlet.handler.SimpleUrlH andlerMapping are rather strange.

    When I try to navigate to HomeController, nothing gets executed.

    I tried many things and mostly followed JavaDoc of WebApplicationInitializer.

    What am I doing wrong?

  • #2
    Originally posted by mezza9 View Post
    @Controller seems to be detected properly (component scan). But warnings on org.springframework.web.servlet.handler.SimpleUrlH andlerMapping are rather strange.
    This is a known issue that has been addressed.

    Originally posted by mezza9 View Post
    When I try to navigate to HomeController, nothing gets executed.
    I think the issue is the servlet mapping. With "/app" you can only map to one path within within Spring MVC and that's "/app". If you enable debug logging you should see the URL the DispatcherServlet uses to find a controller. I recommend using "/" or "/app/*" for the servlet mapping.

    Thanks for trying this out!

    Comment


    • #3
      Originally posted by Rossen Stoyanchev View Post

      I recommend using "/" or "/app/*" for the servlet mapping.
      I took the mapping example from JavaDoc of org.springframework.web.WebApplicationInitializer and did not think about it. So with "/app/*" it works perfectly. Thanks a lot.

      Comment

      Working...
      X