I'm creating a simple hello world application using maven and java-based configuration. However, I keep on getting a 404 error on a simple href link. The files are below.
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home Page</title>
</head>
<body>
<p>This is the Home Page</p>
<a href="hello-page.html">Access Hello</a>
</body>
</html>
LinkController.java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class LinkController {
@RequestMapping(value = "/hello-page")
public ModelAndView showHelloPage(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("hello");
modelAndView.addObject("meow", "MEOW MEOW MEOW");
return modelAndView;
}
}
WebAppConfig.java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
@Configuration //specifies that this is a configuration class
@ComponentScan("com.springtest") //specifies which packages to scan
@EnableWebMvc //specifies that web-mvc annotations can be used
public class WebAppConfig {
@Bean
public UrlBasedViewResolver urlBasedViewResolver(){
UrlBasedViewResolver urlBasedViewResolver = new UrlBasedViewResolver();
urlBasedViewResolver.setPrefix("/WEB-INF/pages/");
urlBasedViewResolver.setSuffix(".jsp");
urlBasedViewResolver.setViewClass(JstlView.class);
return urlBasedViewResolver;
}
}
WebAppInitializer.java
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class WebAppInitializer implements WebApplicationInitializer{
private static final String DISPATCHER_SERVLET_NAME = "dispatcher";
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(WebAppConfig.class);
context.setServletContext(servletContext);
Dynamic servletDynamic = servletContext.addServlet(DISPATCHER_SERVLET_NAME, new DispatcherServlet(context));
//dispatcher servlet will handle all requests
servletDynamic.addMapping("/");
servletDynamic.setLoadOnStartup(1);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>mavenDWP</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
my webapp folder looks like: