未找到Spring MVC HTTP 404 /找不到HTTP 405方法

时间:2015-04-21 22:22:28

标签: java spring jsp spring-mvc

我是Spring MVC的新手。一直试图解决这个问题一段时间(很多个小时)。

我运行此登录表单。我显示正确,但提交时显示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 - &gt;

<?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 - &gt;

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 - &gt;

<!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>  

现在,如果我在web.xml中将<url-pattern>/Login.spring</url-pattern>更改为<url-pattern>*/Login.spring</url-pattern>,那么它会找到405找不到方法 先谢谢你。

此致

2 个答案:

答案 0 :(得分:0)

将您的<url-pattern>/Login.spring</url-pattern>更改为
web.xml中的<url-pattern>*.jsp</url-pattern> 首先尝试在方法级别而不是类级别进行映射,如下所示:
@RequestMapping(value="/Login.spring",method = RequestMethod.POST)

答案 1 :(得分:0)

我的问题的答案是tomcat和jdk版本。

所需的最低配置: Tomcat:v7.0 JDK:1.7.x