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>

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Comments

  1. Excuse me why have you used the htmldatatable here..i dont see you using that any where

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better