I am trying to wire up a basic web site with WebApplicationInitializer. Below is my implementation of the WebApplicationInitializer class, but when I go to run the web site, I get an error:
404 - The requested resource () is not available.
the url: http://localhost:8080/winsgt/WEB-INF...itializer.java
Any suggestions?
the WebMvcConfigurerAdapter:Code:public class WinSGTWebApplicationInitializer implements WebApplicationInitializer { private static final String DISPATCHER_SERVLET_NAME = "dispatcher"; @Override public void onStartup(ServletContext servletContext) throws ServletException { registerListener(servletContext); registerDispatcherServlet(servletContext); } private void registerDispatcherServlet(ServletContext servletContext) { AnnotationConfigWebApplicationContext dispatcherContext = createContext(WebMvcContextConfiguration.class); ServletRegistration.Dynamic dispatcher = servletContext.addServlet(DISPATCHER_SERVLET_NAME, new DispatcherServlet(dispatcherContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); } private void registerListener(ServletContext servletContext) { AnnotationConfigWebApplicationContext rootContext = createContext(InfrastructureContextConfiguration.class); servletContext.addListener(new ContextLoaderListener(rootContext)); } private AnnotationConfigWebApplicationContext createContext(final Class<?>... annotatedClasses) { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.register(annotatedClasses); return context; } }
Code:@Configuration @EnableWebMvc @ComponentScan(basePackages = { "com.apress.prospringmvc.bookstore.web" }) public class WebMvcContextConfiguration extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(final ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**/*").addResourceLocations("classpath:/META-INF/web-resources/"); } @Override public void addViewControllers(final ViewControllerRegistry registry) { registry.addViewController("/index.htm").setViewName("index"); } @Override public void configureDefaultServletHandling(final DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(localeChangeInterceptor()); //registry.addWebRequestInterceptor(commonDataInterceptor()); //registry.addInterceptor(new SecurityHandlerInterceptor()).addPathPatterns("/customer/account", "/cart/checkout"); } //-- Start Locale Support (I18N) --// /** * The {@link LocaleChangeInterceptor} allows for the locale to be changed. It provides a <code>paramName</code> property which sets * the request parameter to check for changing the language, the default is <code>locale</code>. * @return the {@link LocaleChangeInterceptor} */ @Bean public HandlerInterceptor localeChangeInterceptor() { LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor(); localeChangeInterceptor.setParamName("lang"); return localeChangeInterceptor; } /** * The {@link LocaleResolver} implementation to use. Specifies where to store the current selected locale. * * @return the {@link LocaleResolver} */ @Bean public LocaleResolver localeResolver() { return new CookieLocaleResolver(); } /** * To resolve message codes to actual messages we need a {@link MessageSource} implementation. The default * implementations use a {@link java.util.ResourceBundle} to parse the property files with the messages in it. * @return the {@link MessageSource} */ @Bean public MessageSource messageSource() { ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); messageSource.setBasename("classpath:/messages"); messageSource.setUseCodeAsDefaultMessage(true); return messageSource; } @Override public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) { exceptionResolvers.add(simpleMappingExceptionResolver()); } @Bean public SimpleMappingExceptionResolver simpleMappingExceptionResolver() { SimpleMappingExceptionResolver exceptionResolver = new SimpleMappingExceptionResolver(); Properties mappings = new Properties(); mappings.setProperty("AuthenticationException", "login"); Properties statusCodes = new Properties(); mappings.setProperty("login", String.valueOf(HttpServletResponse.SC_UNAUTHORIZED)); exceptionResolver.setExceptionMappings(mappings); exceptionResolver.setStatusCodes(statusCodes); return exceptionResolver; } }


Reply With Quote