java.lang.IllegalStateException:BindingResult 和 bean 名称的普通目标对象都不能用作请求属性
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name available as request attribute
我正在尝试从 spring-jsp 中的控制器获取的列表中填充复选框。我从服务中获取列表,然后将其作为属性添加到模型中。但是我发现很难访问它并将其显示为视图中的复选框。所以我在日志中收到上述错误并且我没有得到视图。可能出了什么问题?
控制器:
@RequestMapping(value = "/user-roles", method = RequestMethod.GET)
public String viewRoles(Model model) {
List<Permission> permissionsList;
permissionsList = roleService.getAllPermission();
model.addAttribute("permissions", permissionsList);
return "user_roles";
}
@RequestMapping(value = "/roles/addrole", method = RequestMethod.POST)
public ModelAndView saveEmployee(@ModelAttribute("addrole") SystemRole role, ModelMap model) {
roleService.saveSystemRole(role);
model.addAttribute("user", new SystemRole());
return new ModelAndView("user_roles");
}
观点:
<div class="modal-body">
<form:form method="post" modelAttribute="addrole" action="/roles/addrole">
<div class="form-group">
<label for="role-name" class="col-form-label">Role Name</label>
<input type="text" class="form-control" id="role-name">
</div>
<div class="form-group">
<label class="col-form-label">Permissions</label>
<div class="check-box" style="padding-left: 20px">
<c:forEach items="${permissions}" var="oo">
<form:checkbox id="${oo.id}" label="${oo.name}" value="${oo.id}" path="permissionList"/>
</c:forEach>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form:form>
</div>
这是复选框表单评论时的视图:
如果您想显示 permission
字段的复选框,请尝试更改
<div class="check-box" style="padding-left: 20px">
<c:forEach items="${permissions}" var="oo">
<form:checkbox id="${oo.id}" label="${oo.name}" value="${oo.id}" path="permissionList"/>
</c:forEach>
</div>
到
<c:forEach items="${permissions}" var="oo">
<div class="checkbox">
<label for="${oo.id}">
<input type="checkbox" name="checkboxes" id="${oo.id}" value="${oo.id}">
${oo.name}
</label>
</div>
</c:forEach>
或
<c:forEach items="${permissions}" var="oo">
<div class="check-box" style="padding-left: 20px">
<form:checkbox id="${oo.id}" label="${oo.name}" value="${oo.id}" path="permissionList"/>
</div>
</c:forEach>
我正在尝试从 spring-jsp 中的控制器获取的列表中填充复选框。我从服务中获取列表,然后将其作为属性添加到模型中。但是我发现很难访问它并将其显示为视图中的复选框。所以我在日志中收到上述错误并且我没有得到视图。可能出了什么问题?
控制器:
@RequestMapping(value = "/user-roles", method = RequestMethod.GET)
public String viewRoles(Model model) {
List<Permission> permissionsList;
permissionsList = roleService.getAllPermission();
model.addAttribute("permissions", permissionsList);
return "user_roles";
}
@RequestMapping(value = "/roles/addrole", method = RequestMethod.POST)
public ModelAndView saveEmployee(@ModelAttribute("addrole") SystemRole role, ModelMap model) {
roleService.saveSystemRole(role);
model.addAttribute("user", new SystemRole());
return new ModelAndView("user_roles");
}
观点:
<div class="modal-body">
<form:form method="post" modelAttribute="addrole" action="/roles/addrole">
<div class="form-group">
<label for="role-name" class="col-form-label">Role Name</label>
<input type="text" class="form-control" id="role-name">
</div>
<div class="form-group">
<label class="col-form-label">Permissions</label>
<div class="check-box" style="padding-left: 20px">
<c:forEach items="${permissions}" var="oo">
<form:checkbox id="${oo.id}" label="${oo.name}" value="${oo.id}" path="permissionList"/>
</c:forEach>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form:form>
</div>
这是复选框表单评论时的视图:
如果您想显示 permission
字段的复选框,请尝试更改
<div class="check-box" style="padding-left: 20px">
<c:forEach items="${permissions}" var="oo">
<form:checkbox id="${oo.id}" label="${oo.name}" value="${oo.id}" path="permissionList"/>
</c:forEach>
</div>
到
<c:forEach items="${permissions}" var="oo">
<div class="checkbox">
<label for="${oo.id}">
<input type="checkbox" name="checkboxes" id="${oo.id}" value="${oo.id}">
${oo.name}
</label>
</div>
</c:forEach>
或
<c:forEach items="${permissions}" var="oo">
<div class="check-box" style="padding-left: 20px">
<form:checkbox id="${oo.id}" label="${oo.name}" value="${oo.id}" path="permissionList"/>
</div>
</c:forEach>