无法从表单中获取字段

时间:2017-04-22 18:05:34

标签: spring forms spring-mvc

所以我对Spring Boot MVC和Thymeleaf完全不熟悉。这就是我想要做的。用户填写并按下提交的表单,在提交时,Spring会创建类型为Spitter的对象,并使用用户在表单中键入的内容填充其字段。在创建它之后,它应该查看结果页面,在该页面中显示用户键入的内容......理论上非常简单。

这就是我现在所拥有的:

控制器:

@Controller
@RequestMapping("/spittles")
public class SpittleController {

Logger log = LoggerFactory.getLogger(this.getClass());

@RequestMapping(value = "/viewMock", method = RequestMethod.GET)
public String spittles(Model model) {
    Spitter spitterObj;
    model.addAttribute("spittle",
            spitterObj = new Spitter(22222, "MOCK", "MOCK"));
    SpitterContainer.containterContaingSpittles.add(spitterObj);
    return "spittles";
}

@RequestMapping(value = "/register", method = RequestMethod.GET)
public String register(Model model) {

    model.addAttribute("spitter", new Spitter());


    return "form";
}

@RequestMapping(value = "/register", method = RequestMethod.POST)
public String registerSubmit(@ModelAttribute Spitter spitter, Model model) {
    String info = String.format("Spitter Submission: id = %s, firstname = %s, lastname = %s",
            spitter.getID(), spitter.getUsername(), spitter.getPassword());
    log.info(info);
    model.addAttribute("spitter", spitter);
    SpitterContainer.containterContaingSpittles.add(new Spitter(44444, spitter.getUsername(), spitter.getPassword()));
    return "result";
}
}

Spitter Class:

public class Spitter {

private int ID;
private String username;
private String password;


public Spitter(){};

public Spitter(int ID, String us, String pwd){
    setID(ID);
    setPassword(pwd);
    setUsername(us);
}

public String getID() {
    return Integer.toString(ID) ;
}

public void setID(int ID) {
    this.ID = ID;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public List<String> getSpittles() {
    return spittles;
}

public void setSpittles(List<String> spittles) {
    this.spittles = spittles;
}

private List<String> spittles;

}

表单视图:

    <!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>REGISTRATION FORM</title>
</head>
    <body>
    <h1>Form</h1>
    <form action="#" th:object="${spitter}" method="post">
        <p>username: <input type="text" th:field="*{username}" /></p>
        <p>password: <input type="text" th:field="*{password}"/></p>
        <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
    </form>

</body>

结果视图:

    <!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Result</h1>
<p th:text="'id: ' + ${spitter.getID}" />
<p th:text="'First Name: ' + ${spitter.getUsername}" />
<p th:text="'Password Name: ' + ${spitter.getPassword}" />
<a href="/form">Submit another Customer Form</a>
</body>
</html>

我知道这是编写得很糟糕的代码,但我想让这个简单的事情发挥作用。日志输出写入详细信息(忽略ID号,但我知道原因),但是我收到以下错误: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'getID' cannot be found on object of type 'hello.data.Spitter' - maybe not public?

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

更改结果视图:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Result</h1>
<p th:text="'id: ' + ${spitter.ID}" />
<p th:text="'First Name: ' + ${spitter.username}" />
<p th:text="'Password Name: ' + ${spitter.password}" />
<a href="/form">Submit another Customer Form</a>
</body>
</html>

您应该使用字段名称,而不是getter方法。