Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7024861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:53:52+00:00 2026-05-27T23:53:52+00:00

I know there have been already questions around this topic, but I have not

  • 0

I know there have been already questions around this topic, but I have not figured out how to solve the following issue:

I have a user/roles relationship and I want to list all available roles in a JSP as checkbox items, where the users assigned checkboxes are selected. However, the matching items are not checked (here I am using Spring 3.1).

Extract from the User object:

private Set<RoleEntity> roles = new HashSet<RoleEntity>();

Extract from the Spring Controller (adding user object and role list to Model):

UserEntity userEntity = userEntityService.findById(UserEntity.class, new Long(id));
model.addAttribute("userAttribute", userEntity);

List<RoleEntity> roleList = roleEntityService.findAll();
model.addAttribute("roleList", roleList);

Extract from the JSP:

<form:form modelAttribute="userAttribute" method="POST" action="${saveUrl}">
...

    <table align="center">
        <tr>
            <td>ID</td>
            <td>Role Name</td>
        </tr>
        <c:forEach items="${roleList}" var="role" varStatus="status">
            <tr>
                <td><form:checkbox path="roles" value="${role}" label="${role.id}" /></td>
                <td><c:out value="${role.name}" /></td>
            </tr>
        </c:forEach>
    </table>

...
</form:form>

The Spring MVC documentation says this:
When the bound value is of type array or java.util.Collection, the input(checkbox) is marked as ‘checked’ if the configured setValue(Object) value is present in the bound Collection.

Isn’t that the case here? What am I missing here?

Thanks a lot.

Paul

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:53:53+00:00Added an answer on May 27, 2026 at 11:53 pm

    My guess is you are missing the implementation for the equals and hashcode methods on the RoleEntity class.

    When the bound value is of type array or java.util.Collection, the input(checkbox) is marked as ‘checked’ if the configured setValue(Object) value is present in the bound Collection.

    This is correct, but to check for presence in a HashSet you need equals and hashcode implemented correctly.

    Just as a quick test to see if that’s the problem, replace this line:

    model.addAttribute("roleList", roleList);
    

    with this line:

    model.addAttribute("roleList", userEntity.getRoles());
    

    Do you get all your checkboxes checked? If yes, then you didn’t provide your own equals and hashcode and the default ones are used (those inherited from Object).

    The default equals compares identity which means that a variable holds the same instance as another variable. Equality means two different object contain the same state or have the same meaning, so to speak.

    Using model.addAttribute("roleList", userEntity.getRoles()) triggers the default equals method to return true because the list and values you check for presence in the list are identical (two identical objects are always equal).

    But in your case you use userEntityService.findById for one and roleEntityService.findAll for the other which means different objects. At this point you have to use a proper equality test as opposed to identity.

    Do you have equals/hashcode implemented?

    Based on your code, here is an example that works:

    Controller:

    import java.util.Arrays;
    import java.util.Collections;
    import java.util.HashSet;
    import java.util.List;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    @Controller
    public class SomeController {
        @RequestMapping(value = "/something", method = { RequestMethod.GET, RequestMethod.POST })
        public String handle(Model model) {
    
            UserEntity userEntity = new UserEntity();
            userEntity.setRoles(new HashSet<RoleEntity>());
            Collections.addAll(userEntity.getRoles(), 
                                    new RoleEntity(1, "one"), 
                                    new RoleEntity(3, "three"));
            model.addAttribute("userAttribute", userEntity);        
    
            List<RoleEntity> roleList = Arrays.asList(
                                            new RoleEntity(1, "one"), 
                                            new RoleEntity(2, "two"), 
                                            new RoleEntity(3, "three")
                                        );
            model.addAttribute("roleList", roleList);
    
            return "view";
        }
    }
    

    User class:

    import java.util.HashSet;
    import java.util.Set;
    
    public class UserEntity {
        private Set<RoleEntity> roles = new HashSet<RoleEntity>();
    
        public Set<RoleEntity> getRoles() {
            return roles;
        }
        public void setRoles(Set<RoleEntity> roles) {
            this.roles = roles;
        }
    }
    

    Roles class (notice the equals and hashcode methods; if you remove them, the example no longer works):

    public class RoleEntity {
        private long id;
        private String name;
    
        @Override
        public int hashCode() {
            return new Long(id).hashCode();
        }
    
        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (! (obj instanceof RoleEntity)) {
                return false;
            }
            return this.id == ((RoleEntity)obj).getId();
        }
    
        public RoleEntity(long id, String name) {
            this.id = id;
            this.name = name;
        }
    
        public long getId() {
            return id;
        }
        public void setId(long id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }
    

    View:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <form:form modelAttribute="userAttribute" method="POST" action="/something">
        <table align="center">
            <tr>
                <td>ID</td>
                <td>Role Name</td>
            </tr>
            <c:forEach items="${roleList}" var="role">
                <tr>
                    <td><form:checkbox path="roles" value="${role}" label="${role.id}" /></td>
                    <td><c:out value="${role.name}" /></td>
                </tr>
            </c:forEach>
        </table>
    </form:form>
    

    P.S. Just one observation about your JSP. If you do value="${role}" for your form:checkbox you will get HTML checkbox attributes like value="your.pack.age.declaration.RoleEntity@1" which might get you in another sort of trouble later on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there have been a lot of questions on this topic already, but
I know there have already been questions very similar to this, but I couldn't
I know there have been some similar questions to this, but they haven't helped
I know there have been a million questions asking this, but mine is different.
I know there have been several posts already about Sudoku-related questions, but I am
I know there has already been a bunch of questions similar to this, but
I know there are tons of threads regarding this issue but I have not
OK, I know there have already been questions about getting started with TDD ..
I know it have been discussed already at some point. But after searching there
I know there have been many questions on grid and pack in the past

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.