Reputation: 67
I keep getting the "Neither BindingResult nor plain target object for bean name 'id' available as request attribute" error when try to add the related user iterating through the loop.
@RequestMapping(value = "/user", method = RequestMethod.GET)
public String getUser(HttpServletRequest request, HttpServletResponse response, Model model, @RequestParam("id") String id, @ModelAttribute("userForm") UserForm userForm)
{
User user = userService.getUser(id);
List<User> relateUsers = userService.getRelatedUsers(id);
userForm.setUser(user);
userForm.setRelateUsers(relateUsers);
return userDetailForm;
}
<div class="trackList" th:if="${userForm.relatedUsers.size() > 0}" th:each="relatedUser : ${userForm.relatedUsers}" >
<th:form method="POST" th:action="@{/user}">
<input type="hidden" id="id" th:field="*{id}"/>
<div>
<a class="add" role="button">Add</a>
</div>
</th:form>
</div>
Upvotes: 1
Views: 1726
Reputation: 67
It helped me to bind value to model attribute, using this code :
<input type="hidden" th:id="id" th:name="id" th:value="*{id}"/>
Upvotes: 0
Reputation:
Your example is weird because your handler just maps to a GET
request. Anyway, your method signature of your POST
handler is invalid in respect to BindingResult
.
Spring Framework documentation says:
org.springframework.validation.Errors / org.springframework.validation.BindingResult validation results for a preceding command or form object (the immediately preceding method argument).
You have to change method signature of your POST handler from something like that
@Valid @ModelAttribute("userForm") UserForm userForm, @RequestParam("id") String id
to
@Valid @ModelAttribute("userForm") UserForm userForm, BindingResult result, @RequestParam("id") String id
Upvotes: 1