Announcement Announcement Module
Collapse
No announcement yet.
Issue when binding multiple boolean fields on Domain object Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Issue when binding multiple boolean fields on Domain object

    My goal is to scaffold some pages to manage user creation for use with Spring Security within a Roo (1.1.0.M1) app. I am trying to write a JPA UserDetails service and started by modelling the UserAccount and UserPermission objects:

    Code:
    package com.spt.authentication.domain;
    
    import java.util.Collection;
    import java.util.HashSet;
    import java.util.Set;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.ManyToMany;
    import javax.validation.constraints.NotNull;
    import javax.validation.constraints.Size;
    
    import org.springframework.roo.addon.entity.RooEntity;
    import org.springframework.roo.addon.javabean.RooJavaBean;
    import org.springframework.roo.addon.tostring.RooToString;
    import org.springframework.security.core.GrantedAuthority;
    import org.springframework.security.core.userdetails.UserDetails;
    
    @Entity
    @RooJavaBean
    @RooToString
    @RooEntity(finders = { "findUserAccountsByEnabled",
    		"findUserAccountsByUsername" })
    public class UserAccount implements UserDetails {
    	private static final long serialVersionUID = 1L;
    
    	@Size(max = 50)
    	@NotNull
    	@Column(unique = true)
    	String username;
    
    	@Size(max = 50)
    	@NotNull
    	String password;
    
    	@NotNull
    	boolean enabled = true;
    
    	@NotNull
    	boolean accountNonExpired = true;
    
    	@NotNull
    	boolean accountNonLocked = true;
    
    	@NotNull
    	boolean credentialsNonExpired = true;
    
    	@ManyToMany(fetch = FetchType.EAGER)
    	Set<UserPermission> permissions = new HashSet<UserPermission>();
    
    	@Override
    	public Collection<GrantedAuthority> getAuthorities() {
    		Collection<GrantedAuthority> converted = new HashSet<GrantedAuthority>();
    		converted.addAll(this.permissions);
    
    		return converted;
    	}
    }
    Code:
    package com.spt.authentication.domain;
    
    import javax.persistence.Entity;
    import javax.validation.constraints.NotNull;
    import javax.validation.constraints.Size;
    
    import org.springframework.roo.addon.entity.RooEntity;
    import org.springframework.roo.addon.javabean.RooJavaBean;
    import org.springframework.roo.addon.tostring.RooToString;
    import org.springframework.security.core.GrantedAuthority;
    
    @Entity
    @RooJavaBean
    @RooToString
    @RooEntity
    public class UserPermission implements GrantedAuthority {
    	private static final long serialVersionUID = 1L;
    	
    	@NotNull
    	@Size(max = 255)
    	String authority;
    }
    Next I performed a controller all --package ~.web to generate out the scaffolding. I get the expected create form produced but no matter what combination of checkboxes are selected, the result is always the same on the persist: The "enabled" flag is always "true" if any of the checkboxes were set to true and all other values are persisted as false. If all checkboxes are deselected, "enabled" is false.

    I performed a test with the first and last checkboxes selected (Enabled and Credentials Non Expired). When I look in the request object I see the following parameters were submitted:
    enabled=true, true
    username=Test
    _enabled=on
    _accountNonExpired=on
    password=Test

    The submitted form has the _enabled value set correctly, but _accountNonExpired was not selected in the form but ended up in the posted data. credentialsNonExpired has gone missing.

    The create.jspx is an un-modified scaffold page:

    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <div xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:field="urn:jsptagdir:/WEB-INF/tags/form/fields" xmlns:form="urn:jsptagdir:/WEB-INF/tags/form" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:spring="http://www.springframework.org/tags" version="2.0">
        <jsp:output omit-xml-declaration="yes"/>
        <form:create id="fc:com.spt.authentication.domain.UserAccount" modelAttribute="userAccount" path="/useraccounts" render="${empty dependencies}" z="4Ycc5CUp/o7jkUuZfed7cFkukAk=">
            <field:textarea field="username" id="c:com.spt.authentication.domain.UserAccount.username" required="true" z="zZFaP+LQtdJ1WuQh0lvm2vtEt+8="/>
            <field:textarea field="password" id="c:com.spt.authentication.domain.UserAccount.password" required="true" z="DlTFuxK6pvFSuLq5SwYbp5wiyLo="/>
            <field:checkbox field="enabled" id="c:com.spt.authentication.domain.UserAccount.enabled" z="6zHSLOB2JFm3bFXVkZcPaIjKQ4g="/>
            <field:checkbox field="accountNonExpired" id="c:com.spt.authentication.domain.UserAccount.accountNonExpired" z="L6WZXig5AQZFz4Tq5HAKYLi12R8="/>
            <field:checkbox field="accountNonLocked" id="c:com.spt.authentication.domain.UserAccount.accountNonLocked" z="PNxPy3oeZUgGUiEUKeyc1ibjNNY="/>
            <field:checkbox field="credentialsNonExpired" id="c:com.spt.authentication.domain.UserAccount.credentialsNonExpired" z="UzCRQw7YRDKlSThf2VWkMuqTBeg="/>
            <field:select field="permissions" id="c:com.spt.authentication.domain.UserAccount.permissions" itemValue="id" items="${userpermissions}" multiple="true" path="/userpermissions" z="xR+ul5zzAtZUGazWd9KCdDba/TI="/>
        </form:create>
        <form:page id="fs:com.spt.authentication.domain.UserAccount" render="${not empty dependencies}" title="UserAccount" z="2VI+MsLD7iSm1LO7pu7aNXjrynA=">
            <spring:message code="entity.dependency.required"/>
            <c:forEach items="${dependencies}" var="dependency">
                <p>
                    <field:reference field="${dependency[0]}" id="s:com.spt.authentication.domain.UserAccount.${dependency[0]}" path="/${dependency[1]}" z="r6LS/ejk/qcvqGj3F1yRmHMDphA="/>
                </p>
            </c:forEach>
        </form:page>
    </div>
    Any insight into how to have multiple boolean fields correctly bound in a create request would be appreciated.

  • #2
    Seems like we have a same issue here.

    jspx page contains 2 field:checkbox which get decorated by Dojo.

    <field:checkbox field="dseChecked" id="c:checkdse" />
    <field:checkbox field="agbChecked" id="c:checkagb" />

    If i check both and submit the request holds, only the first gets resolved correctly.
    _dseChecked=[true, true]
    _agbChecked=[on]



    This seems no to happen if the javascript in field:checkbox is disabled:

    Spring.addDecoration(new Spring.ElementDecoration({elementId : '_${field}_id', widgetType : 'dijit.form.CheckBox'}));

    Comment

    Working...
    X