【问题标题】:Spring MVC form action POST method not redirectingSpring MVC表单动作POST方法不重定向
【发布时间】:2014-12-11 11:14:23
【问题描述】:

我能够从我的 student.jsp 文件中显示我的表单,当我单击提交按钮时,它应该被重定向到 main.jsp 文件,其中包含我在上一页,即。在表格中。但是 main.jsp 页面没有显示,而是我得到了 404。

这里是 NoticesController.java:

package com.mvc.spring;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class NoticesController {

    @RequestMapping(value = "/student", method = RequestMethod.GET)
    public String showHello(Model model)
    {
        return "student";
    }

    @RequestMapping(value = "/main", method = RequestMethod.POST)
    public String form(@ModelAttribute("StudentModel") Student student, Model model)
    {
        System.out.println("Inside form");
        model.addAttribute("Student", student);
        return "main";
    }

}

这是 Student.java:

package com.mvc.spring;

import org.springframework.stereotype.Controller;


public class Student {

    private int id;
    private String name;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }


}

这是我的 student.jsp:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>

<html>
<head>
<title>This is student.jsp</title>
</head>
<body>
    <form action="main.jsp" method="POST">
        ID: <input type="text" name="id"><br /> 
        Name: <input type="text" name="name" /> 
        <input type="submit" value="Submit" />

    </form>
</body>
</html>

这是我的 main.jsp:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Using GET and POST Method to Read Form Data</title>
</head>
<body>
    <p>${Student.id}</p>
    <p>${Student.name}</p>
</body>
</html>

这是我的 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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>WebMVC</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>notices</display-name>
    <servlet-name>notices</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>notices</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <description>Spring Tutorial</description>
  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/spring</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
</web-app>

这是我的notices-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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.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-3.2.xsd">


    <context:component-scan base-package="com.mvc.spring"></context:component-scan>
    <mvc:annotation-driven></mvc:annotation-driven>
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        id="jspViewResolver">
    <property name="prefix" value="/WEB-INF/jsps/"></property>
    <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

这是日志:

org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/notices/] in DispatcherServlet with name 'notices'

【问题讨论】:

    标签: forms jsp spring-mvc servlets redirect


    【解决方案1】:

    我通过以下方式解决了我的问题:

    这是控制器类:

    package com.mvc.spring;
    
    import java.util.List;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;
    
    import com.mvc.spring.database.Student;
    import com.mvc.spring.service.StudentService;
    
    @Controller
    public class NoticesController {
    
        @RequestMapping(value = "/student", method = RequestMethod.GET)
        public ModelAndView showHello(Model model) {
            return new ModelAndView("student", "command", new StudentSazz());
        }
    
        @RequestMapping(value = "/main", method = RequestMethod.POST)
        public String form(@ModelAttribute("StudentModel") StudentSazz student,
                ModelMap model) {
            System.out.println("Inside form");
            model.addAttribute("name", student.getName());
            model.addAttribute("id", student.getId());
             model.addAttribute("student", student);
            return "main";
        }
    
    }
    

    这是 student.jsp 文件:

    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
    <%@ page contentType="text/html;charset=UTF-8" language="java"%>
    
    <html>
    <head>
    <title>This is student.jsp</title>
    </head>
    <body>
        <h2>Student Information</h2>
        <form:form method="POST" action="/notices/main">
            <table>
                <tr>
                    <td><form:label path="name">Name</form:label></td>
                    <td><form:input path="name" /></td>
                </tr>
    
                <tr>
                    <td><form:label path="id">id</form:label></td>
                    <td><form:input path="id" /></td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" value="Submit" /></td>
                </tr>
            </table>
        </form:form>
    </body>
    </html>
    

    这是 main.jsp 文件:

    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Using GET and POST Method to Read Form Data</title>
    </head>
    <body>
        <h2>Submitted Student Information</h2>
       <table>
        <tr>
            <td>Name</td>
            <td>${name}</td>
        </tr>
    
        <tr>
            <td>ID</td>
            <td>${id}</td>
        </tr>
    </table>  
    </body>
    </html>
    

    基本上,我所做的是使用控制器类中的 Student 对象来获取所有参数,并在 .jsp 文件中使用它们,只使用 $ 表示法。这很好用。

    【讨论】:

    • 对我有用的是使用“notices/main”而不是“/notices/main”。 PS: 小心使用method="POST" 用于post 控制器方法method="GET" 用于Get 请求,否则它将找不到请求。它通常发生在初学者身上。
    【解决方案2】:

    如果您像这样更改表单操作会怎样:

    <form action="/main" method="POST">
    

    【讨论】:

      猜你喜欢
      • 2019-02-02
      • 2019-11-26
      • 2021-04-15
      • 2020-01-22
      • 2018-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多