Announcement Announcement Module
Collapse
No announcement yet.
Very basic example of Spring MVC with JavaConfig but controller is never getting call Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Very basic example of Spring MVC with JavaConfig but controller is never getting call

    I am working on a very basic example of Spring MVC with JavaConfig but controller is never getting called?

    I have posted the code at **[email protected]:JohnathanSmith/springmvc-javaconfig.git** if you would like to download the project:

    I dont know why my controller is not getting called this is the first time I am trying to use JavaConfig but if I try to use the tomcat7 plugin for maven I do get an error

    Here is my WebConfig:

    Code:
    @Configuration
        @ComponentScan(basePackages =  {"com.johnathanmsmith.mvc.web"})
        @EnableWebMvc
        public class WebMVCConfig extends WebMvcConfigurationSupport {
        
        	private static final String MESSAGE_SOURCE = "/WEB-INF/classes/messages";
        
        	private static final Logger logger = LoggerFactory.getLogger(WebMVCConfig.class);
        
        	/**
        	 * @return the view resolver
        	 */
        	@Bean
        	public ViewResolver viewResolver() {
        		logger.debug("setting up view resolver");
        
        		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        		viewResolver.setPrefix("/WEB-INF/views/");
        		viewResolver.setSuffix(".jsp");
        		return viewResolver;
        	}
        
        	@Bean(name = "messageSource")
        	public MessageSource configureMessageSource() {
        		logger.debug("setting up message source");
        		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        		messageSource.setBasename(MESSAGE_SOURCE);
        		messageSource.setCacheSeconds(5);
        		messageSource.setDefaultEncoding("UTF-8");
        		return messageSource;
        	}
        
        	@Bean
        	public LocaleResolver localeResolver() {
        		SessionLocaleResolver lr = new org.springframework.web.servlet.i18n.SessionLocaleResolver();
        		lr.setDefaultLocale(Locale.ENGLISH);
        		return lr;
        	}
        
        	@Override
        	public void addResourceHandlers(ResourceHandlerRegistry registry) {
        		logger.debug("setting up resource handlers");
        		registry.addResourceHandler("/resources/").addResourceLocations("/resources/**");
        	}
        
        	@Override
        	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        		logger.debug("configureDefaultServletHandling");
        		configurer.enable();
        	}
        
        	@Override
        	public void addInterceptors(final InterceptorRegistry registry) {
        		registry.addInterceptor(new LocaleChangeInterceptor());
        	}
        
        	@Bean
        	public SimpleMappingExceptionResolver simpleMappingExceptionResolver() {
        		SimpleMappingExceptionResolver b = new SimpleMappingExceptionResolver();
        
        		Properties mappings = new Properties();
        		mappings.put("org.springframework.web.servlet.PageNotFound", "p404");
        		mappings.put("org.springframework.dao.DataAccessException",	"dataAccessFailure");
        		mappings.put("org.springframework.transaction.TransactionException", "dataAccessFailure");
        		b.setExceptionMappings(mappings);
        		return b;
        	}
        }


    Here is my controller:

    Code:
    @Controller
        @RequestMapping("/ask")
        class IndexController {
        
        	private static final Logger logger = LoggerFactory.getLogger(IndexController.class);
        
            @RequestMapping(method = RequestMethod.GET)
            public String displayRequestPage() {
                return "index";
        
            }
        
        
        }
    and here is my web.xml

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
        <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
        
            <!-- Java-based annotation-driven Spring container definition -->
            <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.johnathanmsmith.mvc.web.config</param-value>
            </context-param>
        
            <listener>
                <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
            </listener>
        
        
        
            <display-name>springmvc-javaconfig Web Application</display-name>
        
            <servlet>
            <servlet-name>springmvc-javaconfig</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
            </servlet>
        
            <servlet-mapping>
                <servlet-name>springmvc-javaconfig</servlet-name>
                <url-pattern>/springmvc-javaconfig/*</url-pattern>
            </servlet-mapping>
        </web-app>


    if I run **"mvn tomcat:run"** I get the following error:
    [CODE]
    INFO: Initializing Spring FrameworkServlet 'springmvc-javaconfig'
    Mar 15, 2013 9:45:13 AM org.apache.catalina.core.ApplicationContext log
    SEVERE: StandardWrapper.Throwable
    org.springframework.beans.factory.BeanDefinitionSt oreException: IOException parsing XML document from ServletContext resource [/WEB-INF/springmvc-javaconfig-servlet.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/springmvc-javaconfig-servlet.xml]

  • #2
    Looks like you set the ContextLoaderListener's ApplicationContext to be the one that uses JavaConfig. But the one for the DispatcherServlet is still using XmlWebApplicationContext. And therefore is looking for an xml file in the WEB-INF directory that matches the naming convention of

    WEB-INF/<servletNameHere>-servlet.xml

    I think for using JavaConfig for that takes an init-param in the DispatcherServlet servlet tag. I don't recall off the top of my head. Sorry.

    Mark

    Comment


    • #3
      Here you go

      Code:
      <!-- Declare a Spring MVC DispatcherServlet as usual -->
      <servlet>
          <servlet-name>appServlet</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
              <param-name>contextClass</param-name>
              <param-value>
                  org.springframework.web.context.support.AnnotationConfigWebApplicationContext
              </param-value>
          </init-param>
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>
                  org.package.YouConfigurationAnnotatedClass
              </param-value>
          </init-param>
      </servlet>
      Sorry first posted old packages from Java Config module.

      Mark
      Last edited by bytor99999; Mar 15th, 2013, 01:17 PM. Reason: update

      Comment

      Working...
      X