GeekySelene
GeekySelene

Reputation: 927

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name available as request attribute

Im trying to populate a checkboxes from a list getting from the controller in spring-jsp. Im getting the list from a service and then it is added to the model as an attribute. But im finding it hard to access and display it as checkboxes in the view. So im getting the above error in the log and im not getting the view. What could have been gone possibly wrong?

The controller :

    @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");
}

the view :

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

This is the view when checkbox form is commented:

without checkbox code

with checkbox code

Upvotes: 1

Views: 351

Answers (1)

Avijit Barua
Avijit Barua

Reputation: 3086

If you want to show checkbox for permission field try changing

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

to

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

or

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

Upvotes: 1

Related Questions