Java 我应该用一个字段来创建Spring模型类吗?

Java 我应该用一个字段来创建Spring模型类吗?,java,spring,spring-boot,spring-mvc,Java,Spring,Spring Boot,Spring Mvc,我不知道我是否应该只使用一个字段来创建类,我将使用它作为@modeldattribute从jsp获取数据?例如: public class Age { private int number; public int getNumber() { return number; } public void setNumber(int number) { this.number = number; }} 然后使用spring mvc表单标记填充此模块属性 <form:form

我不知道我是否应该只使用一个字段来创建类,我将使用它作为@modeldattribute从jsp获取数据?例如:

public class Age {
private int number;

public int getNumber() {
    return number;
}

public void setNumber(int number) {
    this.number = number;
}}
然后使用spring mvc表单标记填充此模块属性

<form:form action="processForm" modelAttribute="age">

<form:input path="number"/>
</form:form>

首先,您需要创建一个控制器:

@Controller
public class AgeController {

    // this is to create an instance of Age and set in the model. 
    @RequestMapping("showForm")
    public String showForm(Model model) {
        model.addAttribute("age", new Age());
        return "form"; // return form page.
    }

    // this is to print the age in the display page.
    @RequestMapping("processForm")
    public String processForm(@ModelAttribute("age") Age age) {
        return "display"; // return display page where you will display the data populated in the form page.
    }

}
现在,表单页面应该如下所示:

<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
 <!-- When you enter the data here, Spring will set the data in the Age bean by calling respective setter of the data members -->
 <body>
     <form:form action="processForm" modelAttribute="age">
       Enter an age : <form:input path="number" />
       <input type="submit" value="Submit" />
     </form:form>
 </body>
</html>

输入年龄:
现在,显示页面应该如下所示:

<%@taglib prefix="core" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
 <body>
     The age is : ${age.number} <!-- Here getter is called internally -->
 </body>
</html>

年龄为:${age.number}

单击提交,显示页面将显示数据。

首先,您需要创建一个控制器:

@Controller
public class AgeController {

    // this is to create an instance of Age and set in the model. 
    @RequestMapping("showForm")
    public String showForm(Model model) {
        model.addAttribute("age", new Age());
        return "form"; // return form page.
    }

    // this is to print the age in the display page.
    @RequestMapping("processForm")
    public String processForm(@ModelAttribute("age") Age age) {
        return "display"; // return display page where you will display the data populated in the form page.
    }

}
现在,表单页面应该如下所示:

<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
 <!-- When you enter the data here, Spring will set the data in the Age bean by calling respective setter of the data members -->
 <body>
     <form:form action="processForm" modelAttribute="age">
       Enter an age : <form:input path="number" />
       <input type="submit" value="Submit" />
     </form:form>
 </body>
</html>

输入年龄:
现在,显示页面应该如下所示:

<%@taglib prefix="core" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
 <body>
     The age is : ${age.number} <!-- Here getter is called internally -->
 </body>
</html>

年龄为:${age.number}
单击提交后,显示页面将显示数据