empty object in controller from thymeleaf form

2019-09-09 20:39发布

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>

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 "/";
    }

output once i submit the form -
TestResult [name=, length=, xyz=null]

1条回答
别忘想泡老子
2楼-- · 2019-09-09 21:10

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>
查看更多
登录 后发表回答