Spring-mvc Dispatcher Servlet mapping giving 404 error
http-status-code-404, java, spring, spring-mvc
Solution
@hagrawal - I have managed to get the application working thanks to your comment:
(1.) As per link, either you can register a class using context.register(AppConfig.class); or scan full package using context.setConfigLocation("com.example.app.config");. I see that you can using scan package configuration but specifying a class, so I think either you should use context.setConfigLocation("com.pluralsight"); or context.register("com.pluralsight.WebConfig.class"); or context.register("WebConfig.class"); - hagrawal
The issue was with how my AnnotationConfigWebApplicationContext was registering my "WebConfig" class in my WebAppInitializer Class. I changed from:
context.setConfigLocation("com.pluralsight.WebConfig");
To This:
context.register(com.pluralsight.WebConfig.class);
And the application now finds the correct mapping to respond with. This means my Application is working fully in Java code and has no Web.xml configured anymore! :)
Many thanks for helping and suggesting that to me!
Problem
I am following along with the Pluralsight "Introduction to Spring MVC 4" course, i have done the two previous requested courses as well (Intro to Spring and Intro to Spring MVC). I am not using any XML configurations, it's purely Java/Annotation based. Using the XML Equivalent i can access the "/greeting.html" page with no issue. All other answers on the site involve adding mvc:annotation-driven or a different url-mapping such as "/" or "*.do", which has not helped solve my issue. The index page is displaying upon server startup (localhost:8080), but displays a 404 for localhost:8080/greeting.html. HTTP Status 404 – Not Found Type Status Report Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. Console log shows the following: 16:15:02.072 [http-nio-8080-exec-4] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'eventTrackerDispatcherServlet' processing GET request for [/greeting.html] 16:15:02.078 [http-nio-8080-exec-4] WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/greeting.html] in DispatcherServlet with name 'eventTrackerDispatcherServlet' 16:15:02.078 [http-nio-8080-exec-4] DEBUG org.springframework.web.servlet.DispatcherServlet - Successfully completed request Please advise what configuration i may have missed. WebConfig.java ``` @Configuration @EnableWebMvc @ComponentScan(basePackages = "com.pluralsight") public class WebConfig { @Bean public InternalResourceViewResolver getInternalResourceViewResolver(){ InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/jsp/"); resolver.setSuffix(".jsp"); return resolver; } } ``` WebAppInitializer.java ``` public class WebAppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { WebApplicationContext context = getContext(); servletContext.addListener(new ContextLoaderListener(context)); ServletRegistration.Dynamic dispatcher = servletContext.addServlet("eventTrackerDispatcherServlet", new DispatcherServlet(context)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("*.html"); } private AnnotationConfigWebApplicationContext getContext() { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setConfigLocation("com.pluralsight.WebConfig"); return context; } } ``` HelloController.java ``` @Controller public class HelloController { @RequestMapping(value = "/greeting", method = RequestMethod.GET) public String sayHello(Model model) { model.addAttribute("greeting", "Hello World :)"); return "hello"; } } ``` hello.jsp ``` <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Hello JSP Page</title> </head> <body> <h1>${greeting}</h1> <br /> <h4>This Thing On? :/</h4> </body> </html> ``` I have re-watched the video's to try find my mistake but haven't yet. Many Thanks. Edit 1 - Using XML Based Servlet Mapping, i can access the following page successfully: http://localhost:8080/greeting.html I can access this page using the HelloController code as above in my original post. Greeting.html page - Working with XML configuration After converting my application from XML based configuration to ONLY Java based configuration is when i start receiving the 404 when accessing that page.