简体   繁体   中英

Spring MVC HTTP 404 Not Found / HTTP 405 Method Not Found

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

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)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM