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 **git@github.com: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:
and here is my web.xmlCode:@Controller @RequestMapping("/ask") class IndexController { private static final Logger logger = LoggerFactory.getLogger(IndexController.class); @RequestMapping(method = RequestMethod.GET) public String displayRequestPage() { return "index"; } }
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]


Reply With Quote
