1

I am new to Spring MVC. Have been trying to solve this for a while now (many hours).

I run this login form. I displays right, but when submitted shows 404 Not Found.

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>LoginApp-SpringMVC-Part1</display-name>
  <welcome-file-list>
   <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/Login.spring</url-pattern>
  </servlet-mapping>
</web-app>

dispatcher-servlet.xml -->

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        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">

    <context:component-scan base-package="com.webform.controller" ></context:component-scan>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property
            name="prefix"
            value="/" >
        </property>
        <property
            name="suffix"
            value=".jsp" >
        </property>
    </bean>
  </beans>

LoginController.java -->

package com.webform.controller;



import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/Login.spring")
public class LoginController {
    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView processCredentials(@RequestParam("userName")String userName,@RequestParam("password")String password) {
        String message = "Invalid credentials";
        if(!userName.equals("") && !password.equals("")) {
            if(userName.equals(password)) {
                message = "Welcome " + userName + "!!";
            }
        }
        return new ModelAndView("list","message",message);
    }
}

index.jsp -->

<!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>Login Form</title>  
 </head>  
 <body>  
  <h2>Login</h2>
  <form method="POST" action="Login.spring">
    <table>
        <tr>
            <td>User Name:</td>
            <td><input type="text" name="userName"></td>
        </tr>
        <tr>
            <td>Password:</td>
            <td><input type="password" name="password"></td>
        </tr>
        <tr>  
         <td colspan="2"><input type="submit" value="Submit"/></td>  
        </tr> 
    </table>
  </form>  
   </body>  
</html>  

Now if I change <url-pattern>/Login.spring</url-pattern> to <url-pattern>*/Login.spring</url-pattern> in web.xml, then it gives me 405 Method Not Found Thank you in Advance.

Regards

6
  • 1
    If you're new, I recommend skipping all the legacy bits (web.xml, XML-based context configuration, external containers) and using Spring Boot, which will do all of the setup for you. (Additionally, for this specific case, use Spring Security instead of doing things by hand.) Commented Apr 21, 2015 at 22:28
  • ok. Thanks. will try that as well. Can you help me with this one? Commented Apr 21, 2015 at 22:34
  • You say "it displays right"--with what request address? Turn logging on DispatcherServlet to DEBUG--what does it say? Commented Apr 21, 2015 at 23:01
  • Its shows the form on localhost:8080/webform But 405 on localhost:8080/webform/Login.spring Commented Apr 22, 2015 at 3:37
  • What is the location of your jsp pages? Commented Apr 22, 2015 at 5:09

2 Answers 2

1

Change your <url-pattern>/Login.spring</url-pattern> to
<url-pattern>*.jsp</url-pattern> in web.xml.
And first try to map at method level instead of class level like this:
@RequestMapping(value="/Login.spring",method = RequestMethod.POST)

Sign up to request clarification or add additional context in comments.

3 Comments

Then it is something related to mapping.
Try to modify your structure and code based on this tutorial : crunchify.com/…
Thanks this one helped a lot.. it was my tomcat version.
1

The answer to my problem was the tomcat and jdk version.

Minimum configuration needed: Tomcat : v7.0 JDK : 1.7.x

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.