JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

How to use h:selectBooleanCheckBox within h:dataTable?

April 12, 2008 by Krishna Srinivasan Leave a Comment

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

dataTableCheckBox.jsp

also read:

  • Introduction to JSF
  • JSF Interview Questions
  • Request Processing Lifecycle phases in JSF

[code lang=”html”]

<!–
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>

[/code]

DataTableCheckBoxBean.java

[code lang=”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";
}
}

[/code]

EmployeeDetails.java

[code lang=”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;
}
}

[/code]

faces-config.xml

[code lang=”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>

[/code]

Filed Under: JSF Tagged With: JSF Tags

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.

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved