3
votes

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.

1
Where is greetins.html placed, directly in WebContent or WEB-INF ? Are you able to access your index.jsp, if yes then what is the URL? - hagrawal
@hagrawal The structure is as follows: /WEB-INF/jsp/hello.jsp and /WEB-INF/index.jsp. I can access index page via [link](localhost:8080) and [link](localhost:8080/index.jsp) - kevinhundermark
Where is your greeting.html, for which you are getting 404, as you mentioned in your question. - hagrawal
@RequestMapping(value = "/greeting", method = RequestMethod.GET) Try replacing /greeting with /greeting.html here. - Roman Puchkovskiy
@hagrawal - There is no greeting.html page. The /greeting URL is mapped to the hello.jsp file through the HelloController. I have added the comparable XML configuration above. - kevinhundermark

1 Answers

3
votes

@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!