JSF: selectManyCheckbox

This article will help with jsf selectmanycheckbox:

Screen Shot

user.xhtml

<t:dataTable
	id="userOrganizations"
	value="#{CreateUserController.filteredUserOrganizations}"
	var="userOrganization"
	rowIndexVar="row"
	rowClasses="oddRow, evenRow"
	headerClass="tableHeader">

...

	<t:column>
		<f:facet name="header">
			<h:outputText value="Organization" />
		</f:facet>

		<h:outputText value="#{userOrganization.organization.name}"/>
	</t:column>

	<t:column>
		<f:facet name="header">
			<h:outputText value="Permissions" />
		</f:facet>

		<h:selectManyCheckbox
			id="permissions"
			value="#{userOrganization.selectedRoles}"
			layout="lineDirection">
            <f:selectItems value="#{CreateUserController.allRoles}" />
        </h:selectManyCheckbox>
	</t:column>
</t:dataTable>

applicationContext.xml

<bean id="ca.intelliware.commons.jsf.ConverterFactory"
            class="ca.intelliware.foo.jsf.ConverterFactoryImpl">
        <constructor-arg>
            <map>
                <entry key="ca.intelliware.foo.model.Role">
                    <bean class="ca.intelliware.foo.jsf.RoleConverter">
                        <constructor-arg>
                             <ref bean="ca.intelliware.foo.dao.RoleDao" />
                        </constructor-arg>
                    </bean>
                </entry>
            </map>
        </constructor-arg>
    </bean>

CreateUserController.java

package ca.intelliware.foo.controller;

import java.util.List;
import javax.faces.model.SelectItem;

...

public class CreateUserController extends UserController {

...

    public List<SelectItem> getAllRoles() {
        return SelectUtil.listToSelectList(getAdministrationService().findAllRoles(), false);
    }
}

RoleConverter.java

package ca.intelliware.foo.jsf;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import org.apache.commons.lang.StringUtils;
import ca.intelliware.foo.dao.RoleDao;
import ca.intelliware.foo.model.Role;

public class RoleConverter implements Converter {

    private RoleDao roleDao;

    public RoleConverter(RoleDao roleDao) {
        this.roleDao = roleDao;
    }

    public Object getAsObject(FacesContext context, UIComponent component, String string) throws ConverterException {
        try {
            return StringUtils.isBlank(string) ? null : this.roleDao.findById(Long.valueOf(string));
        } catch (NumberFormatException e) {
            throw new ConverterException(string + " is not a number");
        }
    }

    public String getAsString(FacesContext context, UIComponent component, Object object) {
        return object == null ? null : String.valueOf(((Role) object).getId());
    }
}

RoleDaoImpl.java

package ca.intelliware.foo.dao;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import ca.intelliware.foo.model.Role;

public class RoleDaoImpl extends HibernateDaoSupport implements RoleDao {

    public RoleDaoImpl(SessionFactory sessionFactory) {
        setSessionFactory(sessionFactory);
    }

    public Role findById(Long id) {
        return (Role) getHibernateTemplate().get(Role.class, id);
    }
...

}

UserOrganization.java

package ca.intelliware.foo.model;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name="user_organization")
public class UserOrganization implements Serializable {

...

    private Long id;
    private Set<Role> roles = Collections.synchronizedSet(new HashSet<Role>());

    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

...

    @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
    @JoinTable(name = "user_organization_role", joinColumns = { @JoinColumn(name = "user_organization_id") }, inverseJoinColumns = { @JoinColumn(name = "role_id") })
    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    @Transient
    public Role[] getSelectedRoles() {
        return (Role[]) this.roles.toArray(new Role[0]);
    }

    public void setSelectedRoles(Role[] roles) {
        this.roles.clear();
        this.roles.addAll(Arrays.asList(roles));
    }
}

HTML Output

<table id="registerForm:userOrganizations"><thead><tr><th class="tableHeader"></th><th class="tableHeader">Organization</th><th class="tableHeader">Permissions</th></tr></thead><tbody id="registerForm:userOrganizations:tbody_element">

<tr class="oddRow"><td><input id="registerForm:userOrganizations:0:deleteUserOrganization" name="registerForm:userOrganizations:0:deleteUserOrganization" type="image" src="/foo/images/delete.gif" onclick="if(typeof window.clearFormHiddenParams_registerForm=='function'){clearFormHiddenParams_registerForm('registerForm');}" alt="Delete Organization" /><td>Organization 1</td><td><table id="registerForm:userOrganizations:0:permissions"><tr>		<td><input id="registerForm:userOrganizations:0:permissions:0" type="checkbox" name="registerForm:userOrganizations:0:permissions" checked="checked" value="1" /><label for="registerForm:userOrganizations:0:permissions:0"> User</label></td>		<td><input id="registerForm:userOrganizations:0:permissions:1" type="checkbox" name="registerForm:userOrganizations:0:permissions" checked="checked" value="2" /><label for="registerForm:userOrganizations:0:permissions:1"> Administrator</label></td>		<td><input id="registerForm:userOrganizations:0:permissions:2" type="checkbox" name="registerForm:userOrganizations:0:permissions" checked="checked" value="3" /><label for="registerForm:userOrganizations:0:permissions:2"> Privacy Officer</label></td></tr></table></td></tr>

<tr class="evenRow"><td><input id="registerForm:userOrganizations:1:deleteUserOrganization" name="registerForm:userOrganizations:1:deleteUserOrganization" type="image" src="/foo/images/delete.gif" onclick="if(typeof window.clearFormHiddenParams_registerForm=='function'){clearFormHiddenParams_registerForm('registerForm');}" alt="Delete Organization" /><td>Organization 2</td><td><table id="registerForm:userOrganizations:1:permissions"><tr>		<td><input id="registerForm:userOrganizations:1:permissions:0" type="checkbox" name="registerForm:userOrganizations:1:permissions" checked="checked" value="1" /><label for="registerForm:userOrganizations:1:permissions:0"> User</label></td>		<td><input id="registerForm:userOrganizations:1:permissions:1" type="checkbox" name="registerForm:userOrganizations:1:permissions" checked="checked" value="2" /><label for="registerForm:userOrganizations:1:permissions:1"> Administrator</label></td>		<td><input id="registerForm:userOrganizations:1:permissions:2" type="checkbox" name="registerForm:userOrganizations:1:permissions" checked="checked" value="3" /><label for="registerForm:userOrganizations:1:permissions:2"> Privacy Officer</label></td></tr></table></td></tr>

<tr class="oddRow"><td><input id="registerForm:userOrganizations:2:deleteUserOrganization" name="registerForm:userOrganizations:2:deleteUserOrganization" type="image" src="/foo/images/delete.gif" onclick="if(typeof window.clearFormHiddenParams_registerForm=='function'){clearFormHiddenParams_registerForm('registerForm');}" alt="Delete Organization" /><td>Organization 3</td><td><table id="registerForm:userOrganizations:2:permissions"><tr>		<td><input id="registerForm:userOrganizations:2:permissions:0" type="checkbox" name="registerForm:userOrganizations:2:permissions" value="1" /><label for="registerForm:userOrganizations:2:permissions:0"> User</label></td>		<td><input id="registerForm:userOrganizations:2:permissions:1" type="checkbox" name="registerForm:userOrganizations:2:permissions" value="2" /><label for="registerForm:userOrganizations:2:permissions:1"> Administrator</label></td>		<td><input id="registerForm:userOrganizations:2:permissions:2" type="checkbox" name="registerForm:userOrganizations:2:permissions" value="3" /><label for="registerForm:userOrganizations:2:permissions:2"> Privacy Officer</label></td></tr></table></td></tr>

</tbody></table>

It's only fair to share...
Share on FacebookGoogle+Tweet about this on TwitterShare on LinkedIn

Leave a Reply