I've created a basic Spring MVC web app with Maven. However, I've been struggling with getting constant 404 errors when I'm accessing the /login URL. The /login URL should be mapped to the AuthController servlet and the createLoginForm() method, but unfortunately it fails and I'm ending up with a 404 error.
I'm trying to figure out what is issue that is causing 404 errors and why the /login URL cannot be mapped to the servlet.
Is the problem located in bad configuration of the web.xml or spring-dispatcher-servlet.xml files that prevent the URL to be mapped?
When I'm accessing the / URL, then the index.jsp file is mapped and it's is working fine The login.jsp file is put outside the WEB-INF directory, in the webapp one as well as the index.jsp file.
Thanks in advance.
AuthController.java
package com.github.wjoz.springmvcreview.auth;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class AuthController {
@RequestMapping(value="/login", method=RequestMethod.GET)
public ModelAndView createLoginForm() {
ModelAndView model = new ModelAndView("login");
return model;
}
}
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" 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>springmvcreview</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring-dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:default-servlet-handler/>
<context:component-scan base-package="main.java.com.github.wjoz.springmvcreview.auth" />
<mvc:annotation-driven />
<!-- Tells the location of the view in the project -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE>
<html>
<head>
<title>Welcome to our application. Sign in.</title>
</head>
<body>
<form action="/springmvcreview/login" method="post">
<div>
<label for="username">Username</label>
<input type="text" name="username" id="username">
<label for="password">Password</label>
<input type="password" name="password" id="password">
<button type="submit">Sign in</button>
</div>
</form>
</body>
</html>
InternalResourceViewResolver
is useless. – Sotirios Delimanolisdefault-servlet-handler
for anything? If not, remove it. – Sotirios Delimanolis