forms - Spring MVC 表单操作 POST 方法不重定向

标签 forms jsp spring-mvc servlets redirect

我能够从我的 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;
    }


}

这是我的学生.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'

最佳答案

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

这是 Controller 类:

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>

基本上,我所做的是使用 Controller 类中的 Student 对象来获取所有参数,并在 .jsp 文件中使用它们,仅使用 $ 符号。这工作得很好。

关于forms - Spring MVC 表单操作 POST 方法不重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27421523/

相关文章:

ruby-on-rails - Simple_form 错误 - ActiveRecord::Relation:Class 的未定义方法 `model_name'

java - 无法获取使用 jQuery Popconfirm 插件单击的按钮的名称

javascript - JQuery:表单验证插件 - 验证特定字符串?

java - 如何删除JSP的 native 请求URL?

java - Spring 3 bean 未正确连接

html - 在 HTML 数字输入中显示尾随的小数零

java - 如何将参数传递给jsp :include

java - 正则表达式匹配java中长字符串中的字符

java - Spring MVC 文件上传 - 仅在 IE 中 - 异步 IO 操作失败(1),原因 : RC: 10053

jsp - spring MVC 多文件上传