1 回答

TA貢獻1815條經驗 獲得超10個贊
在您的 HTML 中,您需要為模型屬性使用正確的語法。Spring 抱怨它找不到屬性,id因為您提供的是 string employee,而不是對象。
modelAttribute="employee" --> th:object="${employee}"
此外,您可以合并到:
@Controller //please add this
public class URLController {
@GetMapping({"/", "/index"})
public String index1(Model model){
model.addAttribute("employee",new Employee());
return "index";
}
@PostMapping("/result")
public String result(@ModelAttribute Employee employee){
System.out.print(employee.getName()); //use a logger instead
return "result"; //may want to return a different page name for clarity
}
}
如果您將 HTML 標記更改為:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
最后,您可能希望使用tel電話字段的輸入類型。這樣做將允許為移動用戶顯示自定義鍵盤。
添加回答
舉報