0

ここにform.jspがあります

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add Student</title>
</head>
<body>
    <form:form action="addStudent" commandName="addStudent" method="post">
        <table>
            <tr>
                <td><form:label path="name">Name:</form:label></td> <td><form:input path="name"/></td>
            </tr>
            <tr>
                <td><form:label path="email">Email:</form:label></td> <td><form:input path="email"/></td>
            </tr>
            <tr>
                <td><form:label path="age">Age:</form:label></td> <td><form:input path="age"/></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="Submit"></td>
            </tr>
        </table>
    </form:form>
</body>
</html>

ここにコントローラーメソッドがあります

@RequestMapping(value="/addStudent", method=RequestMethod.POST)
public String addStudent(@ModelAttribute("addStudent") Student student) {
    System.out.println("Saving Info...");
    System.out.println("Name: "+student.getName());
    System.out.println("Email: "+student.getEmail());
    System.out.println("Age: "+student.getAge());
    return "form";
}

学生モデルはこちら

public class Student {

    private String name;
    private String email;
    private int age;
    private int id;

    public Student() {
        super();
    }
}

web.xml や dispatcher-servlet.xml は必要ないと思います。すべてが正しくバインドされています。つまり、commandName は jsp で正しいということです。ModelAttribute はコントローラで正しいのに、なぜコントローラがコントローラに到達しないのですか? :(

助けてください。私はすでにこれで1日の半分を無駄にしています。PS:私はSpringとstackoverflowが初めてです。

編集:

提案の後、私のコードは次のようになります。

コントローラーの方法:

public String addStudent(@ModelAttribute("student") Student student, BindingResult result) {

jsp フォーム:

<form:form action="addStudent" commandName="student" method="post">

編集:

自分で解決し、モデル属性にこのメソッドを追加しました

@ModelAttribute("student")
    public Student getForm() {
        return new Student();
    }
4

1 に答える 1