1
votes

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>
2
The login.jsp file is put outside the WEB-INF directory Then your InternalResourceViewResolver is useless.Sotirios Delimanolis
Thanks for the reply. However, when I move the login.jsp file into the WEB-INF dir, I'm still getting 404 errors.Wjoz
Are you planning to use default-servlet-handler for anything? If not, remove it.Sotirios Delimanolis
Finally, are you specifying your app's context path in your URL?Sotirios Delimanolis
I've removed the default-servlet-handler as I'm not planning to use it. The app's context path is also specified in the URL.Wjoz

2 Answers

1
votes

This is in addition to Shazin's answer

First, put your JSP files inside WEB-INF. There is no reason to have them outside of it, since you're deliberately breaking your view rendering functionality.

Second,

<servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

means that the URL www.somedomain.com/ will load the index page, and all other URLs will start with that. However, your form

<form action="/springmvcreview/login" method="post">

is hitting the URL www.somedomain.com/springmvcreview/login. This does not match up with the controller URL mapping

@RequestMapping(value="/login", method=RequestMethod.GET)
public ModelAndView createLoginForm() {

does not contain a mapping for the form action. So either remove /springmvcreview from the form action, or modify @RequstMapping value to /springmvcreview/login

0
votes

You don't have the contextConfigLocation for your DispatcherServlet. You need that to tell context:component-scan to scan the com.github.wjoz.springmvcreview.auth package and load the AuthController.

<servlet>
    <servlet-name>spring-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value><PATH_TO>/spring-dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

I saw one more issue. You don't need the main.java part in the following;

<context:component-scan base-package="com.github.wjoz.springmvcreview.auth" />