Announcement Announcement Module
Collapse
No announcement yet.
form:checkboxes not displaying properly yet saving properly Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • form:checkboxes not displaying properly yet saving properly

    I have a simple Role class which has a List<Authority>

    Code:
    @Entity
    @Table(name="role")
    public class Role implements RoleInterface {
    	
    	(...)
    	private List<Authority> authorities;
    	
    	(...)
    	
    	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    	@JoinTable(name = "role_x_authority", joinColumns = { @JoinColumn(name = "role_id") }, inverseJoinColumns = { @JoinColumn(name = "authority_id") })
    	public List<Authority> getAuthorities() {
    		return authorities;
    	}
    	
    	public void setAuthorities(List<Authority> authorities) {
    		this.authorities = authorities;
    	}
    Then I have a JSP which allows me to edit roles and their properties:

    HTML Code:
    <form:form method="post" modelAttribute="role">
    <form:errors path="*" cssClass="error" />
    <table>
      <tr>
        <td>${role.id } - Role Id</td>
        <td><form:input disabled="true" path="id" /></td>
      </tr>
      <tr>
        <td>Role Name</td>
        <td><form:input path="name" /></td>
        <td><form:errors path="name" cssClass="error" /></td>
      </tr>
      <tr>
        <td>Role Desc</td>
        <td><form:input path="description" /></td>
        <td><form:errors path="description" cssClass="error" /></td>
      </tr>
      <tr>
        <td>Authorities</td>
        <td><form:checkboxes items="${authorityList}"  path="authorities" itemLabel="name" itemValue="id" delimiter="<br/>" /></td>
        <td><form:errors path="authorities" cssClass="error" /></td>
      </tr>
      <tr>
      	<td>&nbsp;</td>
      	<td><input type="submit" value="Save Role" /></td>
      	<td>&nbsp;</td>
      </tr>
    </table>
    </form:form>
    ${authorityList} above is a full list of all available authorities. I want the ones that are already attached to the Role object to be checked. However, nothing is checked. Saving works properly.

    I have the following PropertyEditor

    Code:
    public class RoleAuthoritiesEditor extends PropertyEditorSupport {
    	
    	(...)
    	
    	@Override
    	public String getAsText(){
    		Authority authority = (Authority) this.getValue();
    		String textVal = authority.getId().toString();
    		return textVal;
    	}
    
    	@Override
    	public void setAsText(String text) throws IllegalArgumentException {
    		Long authorityId = Long.parseLong(text);
    		Authority auth = _userMgmtService.getAuthorityById(authorityId);
    		setValue(auth);
    	}
    }
    What am I missing here?

  • #2
    After some debugging I noticed that one of Spring's classes used by this tag attempts to compare using Authority's equals method. I overrode Authority.equals and hashCode methods, and the checkboxes are displayed properly now.

    While it works, I was under the impression that a custom PropertyEditorSupport class should be sufficient. Anyone can comment on it?

    Comment

    Working...
    X