百里香形式的控制器中的空对象 [英] empty object in controller from thymeleaf form
问题描述
我有一张表格显示使用百里香模板的testResults-
I have a table showing testResults using thymeleaf template -
<tr th:each="testResult,iterationStatus : ${testResults}">
<td th:text="${iterationStatus.count}">1</td>
<td th:text="${testResult.name}">DOMAIN</td>
<td th:text="${testResult.length}">PROCESS</td>
<td>
<form ACTION="#" th:action="@{/deleteName}" th:object="${testResult}" method="POST">
<input type="hidden" th:field="*{name}"/>
<input type="hidden" th:field="*{length}"/>
<button type="submit">submit</button>
</form>
</td>
</tr>
名称和长度在html中显示良好,但是当我提交表单时,控制器将名称和长度作为空值获取. 我在分配值时做错了......
name and length are showing fine in the html, but when i submit the form, controller gets name and length as empty values. What i am doing wrong in assigning the values....
下面是被调用的控制器方法-
below is controller method that gets invoked -
@RequestMapping(method= RequestMethod.POST, value = "/deleteName")
public String deleteName(@Valid @ModelAttribute("testResult") TestResult testResult, BindingResult bindingResult, Model model, HttpServletRequest request) {
System.out.println(testResult.toString());
return "/";
}
我提交表单后输出-
TestResult [name =,length =,xyz = null]
output once i submit the form -
TestResult [name=, length=, xyz=null]
推荐答案
像在这里一样,我无法让th:field
在th:object
内部工作.但是,如果我停止使用th:field
,而是使用th:value
,则表单提交成功.
I couldn't get th:field
to work inside a th:object
, like you're doing here. However, if I stop using th:field
, and use th:value
instead, the form submits successfully.
<tr th:each="testResult : ${testResults}">
<td th:text="${testResult.name}">DOMAIN</td>
<td th:text="${testResult.length}">PROCESS</td>
<td>
<form ACTION="#" th:action="@{/deleteName}" method="POST">
<input type="hidden" name="name" th:value="${testResult.name}"/>
<input type="hidden" name="length" th:value="${testResult.length}"/>
<button type="submit">submit</button>
</form>
</td>
</tr>
这篇关于百里香形式的控制器中的空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!