亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 JSP 轉換為 Thymleaf:屬性名稱不能為 Null 或空

將 JSP 轉換為 Thymleaf:屬性名稱不能為 Null 或空

慕后森 2023-07-19 16:25:44
我目前正在將許多.jsp頁面轉換為HTML使用Thymeleaf.?我已經成功轉換了其中一些,但是在檢查帶有表單標簽的任何頁面時,我收到以下錯誤:Attribute?name?cannot?be?null?or?empty?during?the?initial?page?load.查看代碼? ?<!-- Registration Form -->? ? ? ? ? ? <form action="#" th:action="@{/register/processRegistrationForm}" th:object="${user}" method="POST" class="form-horizontal">? ? <!-- User name -->? ? ? ? ? ? ? ? ? ? <div style="margin-bottom: 25px" class="input-group">? ? ? ? ? ? ? ? ? ? ? ? <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>?? ? ? ? ? ? ? ? ? ? ? ? <input type="text" th:field:="*{userName}" placeholder="Username (*)" class="form-control" />? ? ? ? ? ? ? ? ? ? </div>? ? ? ? ? ? ? ? ? ? <!-- Password -->? ? ? ? ? ? ? ? ? ? <div style="margin-bottom: 25px" class="input-group">? ? ? ? ? ? ? ? ? ? ? ? <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>?? ? ? ? ? ? ? ? ? ? ? ? <input type="text" th:field:="*{password}" placeholder="First Name (*)" class="form-control" />? ? ? ? ? ? ? ? ? ? </div>控制器代碼@GetMapping("/showRegistrationForm")public String showMyRegistrationPage(? ? ? ? Model theModel) {? ? theModel.addAttribute("user", new UserRegistrationDto());? ? return "Login/registration-form";}@PostMapping("/processRegistrationForm")public String processRegistrationForm(? ? ? ? ? ? @Valid @ModelAttribute ("user") UserRegistrationDto userDto,?? ? ? ? ? ? BindingResult theBindingResult,?? ? ? ? ? ? Model theModel) {? ? String userName = userDto.getUserName();? ? logger.info("Processing registration form for: " + userName);? ? // form validation? ? ?if (theBindingResult.hasErrors()){? ? ? ? ?return "Login/registration-form";? ? ? ? }
查看完整描述

3 回答

?
森欄

TA貢獻1810條經驗 獲得超5個贊

我認為問題出在這兩行上:


<input type="text" th:field:="*{userName}" placeholder="Username (*)" class="form-control" />


<input type="text" th:field:="*{password}" placeholder="First Name (*)" class="form-control" />

:由于您在后面添加了“”列,th:field因此可能會出現錯誤。所以這兩行應該是這樣的:


<input type="text" th:field="*{userName}" placeholder="Username (*)" class="form-control" />


<input type="text" th:field="*{password}" placeholder="First Name (*)" class="form-control" />

由于 thymeleaf 沒有提供對問題或有問題的行號的良好解釋,因此查找問題確實變得很頭疼。它只是說“屬性名稱不能為空”,而您只是搜索空屬性。


查看完整回答
反對 回復 2023-07-19
?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

我遇到了同樣的問題,我只是將錯誤減少 1 1。所以我對每個輸入和每個表單都進行了評論。而問題就在這里

<form action="#" th:action="@{/register/processRegistrationForm}" th:object="${user}" method="POST" class="form-horizontal">

th:object="${user}",也許這個技巧可以幫助你一點,因為這個問題還沒有解決。


查看完整回答
反對 回復 2023-07-19
?
繁星coding

TA貢獻1797條經驗 獲得超4個贊

也許晚了,但我想我明白問題所在了。

th:field:="*{password}"

:太多

應該

th:field="*{password}"


查看完整回答
反對 回復 2023-07-19
  • 3 回答
  • 0 關注
  • 216 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號