How to use h:selectBooleanCheckBox within h:dataTable?

  • Topic : Java Server Faces (JSF)
  • Environment : J2EE 5.0, MyFaces 1.1.5

dataTableCheckBox.jsp

also read:


<!--
   Source : www.javabeat.net
-->
<html>
    <body>
        <f:view>
            <h:form id="select">
                <h:dataTable binding="#{dataTableCheckBoxBean.dataTable}"
                             value="#{dataTableCheckBoxBean.empDetails}" var="loc">
                    <h:column>
                        <h:selectBooleanCheckbox value="#{loc.selected}"/>
                        <h:outputText value="#{loc.empNo}"/>
                    </h:column>
                    <h:column>
                        <h:outputText value="#{loc.empName}"/>
                    </h:column>
                </h:dataTable>
                <h:commandButton value="Submit" action="#{dataTableCheckBoxBean.submit}"/>

            </h:form>
        </f:view>
    </body>
</html>

DataTableCheckBoxBean.java


/**
 * Source : www.javabeat.net
 * */
package net.javabeat.myfaces.data;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.faces.component.html.HtmlDataTable;
import net.javabeat.myfaces.beans.EmployeeDetails;

public class DataTableCheckBoxBean {
    private List<employeeDetails> empDetails;
    private HtmlDataTable dataTable;
    private EmployeeDetails employeeDetails = new EmployeeDetails();
    public DataTableCheckBoxBean(){
        empDetails = new ArrayList<employeeDetails>();
        EmployeeDetails employeeDetails1 = new EmployeeDetails();
        employeeDetails1.setEmpNo(1);
        employeeDetails1.setEmpName("Krishna");
        EmployeeDetails employeeDetails2 = new EmployeeDetails();
        employeeDetails2.setEmpNo(2);
        employeeDetails2.setEmpName("Shunmuga Raja");
        EmployeeDetails employeeDetails3 = new EmployeeDetails();
        employeeDetails3.setEmpNo(3);
        employeeDetails3.setEmpName("MuthuKumar");
        empDetails.add(employeeDetails1);
        empDetails.add(employeeDetails2);
        empDetails.add(employeeDetails3);
    }
    public HtmlDataTable getDataTable() {
        return dataTable;
    }

    public void setDataTable(HtmlDataTable dataTable) {
        this.dataTable = dataTable;
    }

    public List<employeeDetails> getEmpDetails() {
        return empDetails;
    }

    public void setEmpDetails(List<employeeDetails> empDetails) {
        this.empDetails = empDetails;
    }
    public String submit(){
        Iterator iterator = empDetails.iterator();
        while (iterator.hasNext())
        {
            employeeDetails = (EmployeeDetails)iterator.next();
            System.out.print(employeeDetails.isSelected());
            System.out.print(employeeDetails.getEmpNo());
            System.out.println(employeeDetails.getEmpName());
        }
        return "dataTableCheckBox";
    }
}

EmployeeDetails.java

/**
 * Source : www.javabeat.net
 * */
package net.javabeat.myfaces.beans;
public class EmployeeDetails {
    private boolean selected;
    private Integer empNo;
    private String empName;

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }
    private Address empAddress;

    public Integer getEmpNo() {
        return empNo;
    }

    public void setEmpNo(Integer empNo) {
        this.empNo = empNo;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public Address getEmpAddress() {
        return empAddress;
    }

    public void setEmpAddress(Address empAddress) {
        this.empAddress = empAddress;
    }
}

faces-config.xml


    <managed-bean>
        <managed-bean-name>
            dataTableCheckBoxBean
        </managed-bean-name>
        <managed-bean-class>
            net.javabeat.myfaces.data.DataTableCheckBoxBean
        </managed-bean-class>
        <managed-bean-scope>
            request
        </managed-bean-scope>
    </managed-bean>

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>