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.
greetins.htmlplaced, directly in WebContent or WEB-INF ? Are you able to access yourindex.jsp, if yes then what is the URL? - hagrawalgreeting.html, for which you are getting 404, as you mentioned in your question. - hagrawal@RequestMapping(value = "/greeting", method = RequestMethod.GET)Try replacing/greetingwith/greeting.htmlhere. - Roman Puchkovskiy