既不是BindingResult也不是bean名称的明确目标对象' id'可用作请求属性

时间:2016-03-24 02:52:47

标签: spring-mvc thymeleaf

我一直得到" BindingResult和bean名称的简单目标对象' id'可用作请求属性"尝试添加迭代循环的相关用户时出错。

 @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>

2 个答案:

答案 0 :(得分:1)

您的示例很奇怪,因为您的处理程序只是映射到GET请求。无论如何,POST处理程序的方法签名在BindingResult

方面无效

Spring Framework documentation说:

  

org.springframework.validation.Errors / org.springframework.validation.BindingResult前面的命令或表单对象(前一个方法参数)的验证结果。

您必须更改POST处理程序的方法签名

@Valid @ModelAttribute("userForm") UserForm userForm, @RequestParam("id") String id

@Valid @ModelAttribute("userForm") UserForm userForm, BindingResult result, @RequestParam("id") String id

答案 1 :(得分:0)

它帮助我使用以下代码将值绑定到模型属性:

<input type="hidden" th:id="id" th:name="id" th:value="*{id}"/>