0
所以我對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類:
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?
任何想法?
'spitter.ID'或'spitter.iD'而不是'spitter.getID'如何? –