0

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]

2
  • Please post the controller code. At a minimum, post the controller method declaration and annotations. Commented Feb 3, 2016 at 19:30
  • added controller details, thnx. Commented Feb 3, 2016 at 19:52

1 Answer 1

2

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> 
Sign up to request clarification or add additional context in comments.

4 Comments

It works although makes me skeptical about using thymeleaf templates. Thanks
@chappalprasad: I'll spend a few minutes later today digging into why it isn't working with th:field. I wanted to give you a fix as soon as possible, in case your problem was blocking you from getting things done.
I spend some more time looking though the thymeleaf code. It appears the th:field code binds differently than th:value does. th:field requires the th:object you're working with to be directly on the model. In your case, "testResult" is only in your th:each scope. I'm unsure why they work differently, but that's why it doesn't work for you here.
Thanks for investigating further. th:value took care of my needs so i am good.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.