<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Free practice test , mock test, driving test, interview questions &#187; h:datatable example</title>
	<atom:link href="http://www.skill-guru.com/blog/tag/hdatatable-example/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.skill-guru.com/blog</link>
	<description>Find free mock and practice test, create and sell tests</description>
	<lastBuildDate>Fri, 10 Sep 2010 20:32:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Edit, Delete and Clear records using JSF datatable</title>
		<link>http://www.skill-guru.com/blog/2009/08/20/edit-delete-and-clear-records-using-jsf-datatable/</link>
		<comments>http://www.skill-guru.com/blog/2009/08/20/edit-delete-and-clear-records-using-jsf-datatable/#comments</comments>
		<pubDate>Thu, 20 Aug 2009 22:22:25 +0000</pubDate>
		<dc:creator>smitha</dc:creator>
				<category><![CDATA[Programming / tutorials]]></category>
		<category><![CDATA[add record to datatable]]></category>
		<category><![CDATA[delete record from datatable]]></category>
		<category><![CDATA[h:datatable]]></category>
		<category><![CDATA[h:datatable example]]></category>
		<category><![CDATA[jsf]]></category>
		<category><![CDATA[jsf datatable]]></category>

		<guid isPermaLink="false">http://www.skill-guru.com/blog/?p=453</guid>
		<description><![CDATA[We use JSF datatable to display Table of information. We can also use the datatable to edit / delete and add new records. Below I have explained how I have done it using h:dataTable.
Here I assume that you already have a JSF application running. If you are new to JSF and would like to setup [...]]]></description>
			<content:encoded><![CDATA[<p>We use JSF datatable to display Table of information. We can also use the datatable to edit / delete and add new records. Below I have explained how I have done it using h:dataTable.</p>
<p>Here I assume that you already have a JSF application running. If you are new to JSF and would like to setup a freash project on JSF on eclipse please go through the JSF Beginners tutorial.<br />
<a href="http://www.skill-guru.com/blog/2009/08/10/319/"><strong>JSF Tutorials for beginners </strong></a><span id="more-453"></span></p>
<p>The application explained here displays employee data (name and address) in a datatable. User can also select records from the table and add / edit / delete records also. I am using ajax4Jsf library for the effect of selecting a row.</p>
<p>The final jsf page works as follows:<br />
User can click on column named ‘Select One’. The data gets populated to the below table. User can edit or delete the record. If user edits the record he clicks save button to save it. If user wants to delete the record he will click delete. If user wants to enter new row, he can click cancel (if any row has been selected) and enter new record and save it.The new record will appear in the table.</p>
<h2><span style="color: #3366ff;">Setup: </span><strong>Ajax4Jsf Configuration </strong></h2>
<p><strong>Dowload: </strong></p>
<p><strong>ajax4jsf.jar (</strong><a href="http://labs.jboss.com/portal/jbossajax4jsf/downloads" target="_top">http://labs.jboss.com/portal/jbossajax4jsf/downloads</a>)</p>
<p>And  <strong>oscache-2.2.jar (<a href="http://www.mmbase.org/download/jars/oscache-2.2.jar">http://www.mmbase.org/download/jars/oscache-2.2.jar</a>)</strong></p>
<p>Copy them to the web-inf/lib directory.</p>
<p>Open web.xml file and include following in appropriate places:</p>
<blockquote><p>&lt;context-param&gt;</p>
<p>&lt;param-name&gt;org.ajax4jsf.SKIN&lt;/param-name&gt;</p>
<p>&lt;param-value&gt;DEFAULT&lt;/param-value&gt;</p>
<p>&lt;/context-param&gt;</p>
<p>&lt;filter&gt;</p>
<p>&lt;display-name&gt;Ajax4jsf Filter&lt;/display-name&gt;</p>
<p>&lt;filter-name&gt;ajax4jsf&lt;/filter-name&gt;</p>
<p>&lt;filter-class&gt;org.ajax4jsf.Filter&lt;/filter-class&gt;</p>
<p>&lt;/filter&gt;</p>
<p>&lt;filter-mapping&gt;</p>
<p>&lt;filter-name&gt;ajax4jsf&lt;/filter-name&gt;</p>
<p>&lt;url-pattern&gt;/*&lt;/url-pattern&gt;</p>
<h3>&lt;/filter-mapping&gt;</h3>
</blockquote>
<h3><span style="color: #3366ff;">JSF Beans:</span></h3>
<p>Create a package named: com.myapp.example. Create a bean called EmployeeBean like below:</p>
<blockquote><p><strong>package</strong> com.myapp.example;</p>
<p><strong>public</strong> <strong>class</strong> EmployeeBean {</p>
<p><strong>private</strong> String name;</p>
<p><strong>private</strong> String address;</p>
<p><strong>private</strong> Integer empId;</p>
<p><strong>public</strong> EmployeeBean(){}</p>
<p><strong>public</strong> EmployeeBean(Integer empId,String name, String address){</p>
<p><strong>this</strong>.name = name;</p>
<p><strong>this</strong>.address = <span style="text-decoration: underline;">address</span>;</p>
<p><strong>this</strong>.empId = empId;</p>
<p>}</p>
<p><strong>public</strong> EmployeeBean(EmployeeBean bean){</p>
<p><strong>this</strong>.empId = bean.getEmpId();</p>
<p><strong>this</strong>.name = bean.getName();</p>
<p><strong>this</strong>.address = bean.getAddress();</p>
<p>}</p>
<p><strong>public</strong> String getName() {</p>
<p><strong>return</strong> name;</p>
<p>}</p>
<p><strong>public</strong> <strong>void</strong> setName(String name) {</p>
<p><strong>this</strong>.name = name;</p>
<p>}</p>
<p><strong>public</strong> String getAddress() {</p>
<p><strong>return</strong> address;</p>
<p>}</p>
<p><strong>public</strong> <strong>void</strong> setAddress(String address) {</p>
<p><strong>this</strong>.address = address;</p>
<p>}</p>
<p><strong>public</strong> Integer getEmpId() {</p>
<p><strong>return</strong> empId;</p>
<p>}</p>
<p><strong>public</strong> <strong>void</strong> setEmpId(Integer empId) {</p>
<p><strong>this</strong>.empId = empId;</p>
<p>}</p>
<p>}</p></blockquote>
<p>Now create a Managed bean called EmployeeActionBean and copy contents  below:</p>
<blockquote><p>package com.myapp.example;</p>
<p>import java.util.List;</p>
<p>import java.util.ArrayList;</p>
<p>import javax.faces.component.html.HtmlDataTable;</p>
<p>import javax.faces.context.FacesContext;</p>
<p>import javax.faces.application.FacesMessage;</p>
<p>public class EmployeeActionBean {</p>
<p>List&lt;EmployeeBean&gt; employees;</p>
<p>HtmlDataTable table;</p>
<p>int selectedRowIndex = -1;</p>
<p>EmployeeBean currentEmp = new EmployeeBean();</p>
<p>public HtmlDataTable getTable() {</p>
<p>return table;</p>
<p>}</p>
<p>public void setTable(HtmlDataTable table) {</p>
<p>this.table = table;</p>
<p>}</p>
<p>public EmployeeActionBean(){</p>
<p>employees = new ArrayList&lt;EmployeeBean&gt;();</p>
<p>employees.add(new EmployeeBean(1,&#8221;Mark&#8221;,&#8221;A666/88, XX Street, Pune&#8221;));</p>
<p>employees.add(new EmployeeBean(2,&#8221;Smitha&#8221;,&#8221;6/88, AA Street, Delhi&#8221;));</p>
<p>employees.add(new EmployeeBean(3,&#8221;Babita&#8221;,&#8221;BB/88, BB Street, Chennai&#8221;));</p>
<p>employees.add(new EmployeeBean(4,&#8221;Rajesh&#8221;,&#8221;098, CC Street, Mangalore&#8221;));</p>
<p>employees.add(new EmployeeBean(5,&#8221;Rina&#8221;,&#8221;77, DDD Street, Bangalore&#8221;));</p>
<p>employees.add(new EmployeeBean(6,&#8221;Rita&#8221;,&#8221;88, EE Street, Hydrabad&#8221;));</p>
<p>employees.add(new EmployeeBean(7,&#8221;Ramya&#8221;,&#8221;6, FF Street, Pune&#8221;));</p>
<p>}</p>
<p>public List&lt;EmployeeBean&gt; getEmployees() {</p>
<p>return employees;</p>
<p>}</p>
<p>public void setEmployees(List&lt;EmployeeBean&gt; employees) {</p>
<p>this.employees = employees;</p>
<p>}</p>
<p>public int getSelectedRowIndex() {</p>
<p>return selectedRowIndex;</p>
<p>}</p>
<p>public void setSelectedRowIndex(int selectedRowIndex) {</p>
<p>this.selectedRowIndex = selectedRowIndex;</p>
<p>}</p>
<p>public void setSelectedItem() {</p>
<p>String rowIndex = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(&#8220;selectedRowIndex&#8221;);</p>
<p>int iRowIndx = Integer.parseInt(rowIndex);</p>
<p>selectedRowIndex = iRowIndx;</p>
<p>table.setRowIndex(iRowIndx);</p>
<p>EmployeeBean empBeanTmp = (EmployeeBean) table.getRowData();</p>
<p>//if(questionBeanTmp.getAnswers() == null || questionBeanTmp.getAnswers().size() == 0){</p>
<p>try{</p>
<p>currentEmp = new EmployeeBean(empBeanTmp); // creating new for edit.. will not affect origional</p>
<p>}catch(Exception e){</p>
<p>e.printStackTrace();</p>
<p>}</p>
<p>}</p>
<p>public String saveUpdateAction(){</p>
<p>if(currentEmp.getName() == null || currentEmp.getName().trim().equals(&#8220;&#8221;)){</p>
<p>FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_ERROR, &#8220;Name Required!&#8221;,&#8221;Please Enter Name&#8221;));</p>
<p>return &#8220;error&#8221;;</p>
<p>}</p>
<p>if(currentEmp.getAddress() == null || currentEmp.getAddress().trim().equals(&#8220;&#8221;)){</p>
<p>FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_ERROR, &#8220;Address Required!&#8221;,&#8221;Please Enter Address&#8221;));</p>
<p>return &#8220;error&#8221;;</p>
<p>}</p>
<p>if(currentEmp.getEmpId() == null){</p>
<p>Integer i = (employees.get(employees.size() -1)).getEmpId()+1;</p>
<p>employees.add(new EmployeeBean(i,currentEmp.getName(),currentEmp.getAddress()));</p>
<p>currentEmp = new EmployeeBean();</p>
<p>selectedRowIndex = -1;</p>
<p>}else{</p>
<p>for(EmployeeBean emp:employees){</p>
<p>if(emp.getEmpId() == currentEmp.getEmpId())</p>
<p>{</p>
<p>EmployeeBean emptmp = emp;</p>
<p>emptmp.setName(currentEmp.getName());</p>
<p>emptmp.setAddress(currentEmp.getAddress());</p>
<p>}</p>
<p>}</p>
<p>}</p>
<p>return &#8220;success&#8221;;</p>
<p>}</p>
<p>public String DeleteAction(){</p>
<p>if(currentEmp.getEmpId() == null || currentEmp.getEmpId()== 0 ){</p>
<p>FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_ERROR, &#8220;Selection Required!&#8221;,&#8221;Please Select a Record&#8221;));</p>
<p>return &#8220;error&#8221;;</p>
<p>}</p>
<p>for(int i=0;i&lt;employees.size();i++){</p>
<p>if(employees.get(i).getEmpId() == currentEmp.getEmpId())</p>
<p>{</p>
<p>employees.remove(i);</p>
<p>break;</p>
<p>}</p>
<p>}</p>
<p>currentEmp = new EmployeeBean();</p>
<p>selectedRowIndex = -1;</p>
<p>return &#8220;success&#8221;;</p>
<p>}</p>
<p>public void ClearAction(){</p>
<p>currentEmp = new EmployeeBean();</p>
<p>selectedRowIndex = -1;</p>
<p>}</p>
<p>public EmployeeBean getCurrentEmp() {</p>
<p>return currentEmp;</p>
<p>}</p>
<p>public void setCurrentEmp(EmployeeBean currentEmp) {</p>
<p>this.currentEmp = currentEmp;</p>
<p>}</p>
<p>}</p></blockquote>
<p><strong> </strong></p>
<h3><span style="color: #3366ff;">Configure the Bean</span></h3>
<p>Enter following in the Faces-config.xml inside &lt;faces-config&gt; tag.</p>
<blockquote><p>&lt;managed-bean&gt;</p>
<p>&lt;managed-bean-name&gt;employeeActionBean&lt;/managed-bean-name&gt;</p>
<p>&lt;managed-bean-class&gt;com.myapp.example.EmployeeActionBean&lt;/managed-bean-class&gt;</p>
<p>&lt;managed-bean-scope&gt;session&lt;/managed-bean-scope&gt;</p>
<p>&lt;/managed-bean&gt;</p></blockquote>
<p>Create a file named employeeAction.jsp  and copy the below contents:</p>
<blockquote><p>&lt;%@taglib uri=&#8221;http://java.sun.com/jsf/html&#8221; prefix=&#8221;h&#8221;%&gt;</p>
<p>&lt;%@taglib uri=&#8221;http://java.sun.com/jsf/core&#8221; prefix=&#8221;f&#8221;%&gt;</p>
<p>&lt;%@ taglib uri=&#8221;https://ajax4jsf.dev.java.net/ajax&#8221; prefix=&#8221;a4j&#8221;%&gt;</p>
<p>&lt;html&gt;</p>
<p>&lt;body bgcolor=&#8221;#FFFFFF&#8221; text=&#8221;#000000&#8243; topmargin=&#8221;0&#8243;&gt;</p>
<p>&lt;f:view&gt;</p>
<p>&lt;h:form id=&#8221;frmEditEmployees&#8221;&gt;</p>
<p>&lt;table border=&#8221;0&#8243; cellspacing=&#8221;0&#8243; cellpadding=&#8221;0&#8243;&gt;</p>
<p>&lt;tr &gt;</p>
<p>&lt;td colspan=&#8221;2&#8243;&gt;</p>
<p>&lt;h:messages id=&#8221;msg1&#8243; errorStyle=&#8221;color:red;font-family:Tahoma;font-size:12px;font-weight:bold;&#8221;</p>
<p>infoStyle=&#8221;color:blue;font-family:Tahoma;font-size:12px;font-weight:bold;&#8221;  /&gt;</p>
<p>&lt;/td&gt;</p>
<p>&lt;/tr&gt;</p>
<p>&lt;tr&gt;</p>
<p>&lt;td width=&#8221;100%&#8221;&gt;</p>
<p>&lt;h:dataTable id=&#8221;empTable&#8221;</p>
<p>value=&#8221;#{employeeActionBean.employees}&#8221;</p>
<p>binding=&#8221;#{employeeActionBean.table}&#8221;</p>
<p>var=&#8221;emp&#8221;</p>
<p>&gt;</p>
<p>&lt;h:column&gt;</p>
<p>&lt;f:facet name=&#8221;header&#8221; &gt;</p>
<p>&lt;h:outputText style=&#8221;font-size:12px;&#8221; value=&#8221;is Selected&#8221; id=&#8221;h0&#8243;/&gt;</p>
<p>&lt;/f:facet&gt;</p>
<p>&lt;h:graphicImage</p>
<p>value=&#8221;/checkmark.gif&#8221;</p>
<p>alt=&#8221;Current Selection&#8221;</p>
<p>rendered=&#8221;#{employeeActionBean.table.rowIndex==guruTestsActionBean.selectedRowIndex}&#8221;</p>
<p>id=&#8221;selectedicon&#8221;&gt;&lt;/h:graphicImage&gt;</p>
<p>&lt;/h:column&gt;</p>
<p>&lt;h:column&gt;</p>
<p>&lt;f:facet name=&#8221;header&#8221; &gt;</p>
<p>&lt;h:outputText style=&#8221;font-size:12px;&#8221; value=&#8221;Select One&#8221; id=&#8221;h1&#8243;/&gt;</p>
<p>&lt;/f:facet&gt;</p>
<p>&lt;a4j:commandLink  action=&#8221;#{employeeActionBean.setSelectedItem}&#8221;</p>
<p>rendered=&#8221;#{employeeActionBean.table.rowIndex!=employeeActionBean.selectedRowIndex}&#8221;</p>
<p>reRender=&#8221;empTable,detailtab&#8221; &gt;</p>
<p>&lt;h:graphicImage value=&#8221;/edit_icon.gif&#8221;    alt=&#8221;Click here to edit the record&#8221; /&gt;</p>
<p>&lt;f:param name=&#8221;selectedRowIndex&#8221; value=&#8221;#{employeeActionBean.table.rowIndex}&#8221;/&gt;</p>
<p>&lt;/a4j:commandLink&gt;</p>
<p>&lt;/h:column&gt;</p>
<p>&lt;h:column&gt;</p>
<p>&lt;f:facet name=&#8221;header&#8221; &gt;</p>
<p>&lt;h:outputText style=&#8221;font-size:12px;&#8221; value=&#8221;Employee Name&#8221; id=&#8221;h2&#8243;/&gt;</p>
<p>&lt;/f:facet&gt;</p>
<p>&lt;h:outputText style=&#8221;font-size:12px;&#8221; value=&#8221;#{emp.name}&#8221; /&gt;</p>
<p>&lt;/h:column&gt;</p>
<p>&lt;h:column&gt;</p>
<p>&lt;f:facet name=&#8221;header&#8221; &gt;</p>
<p>&lt;h:outputText style=&#8221;font-size:12px;&#8221; value=&#8221;Employee Address&#8221; id=&#8221;h3&#8243;/&gt;</p>
<p>&lt;/f:facet&gt;</p>
<p>&lt;h:outputText style=&#8221;font-size:12px;&#8221; value=&#8221;#{emp.address}&#8221; /&gt;</p>
<p>&lt;/h:column&gt;</p>
<p>&lt;/h:dataTable&gt;</p>
<p>&lt;/td&gt;</p>
<p>&lt;/tr&gt;</p>
<p>&lt;tr&gt;</p>
<p>&lt;td&gt;</p>
<p>&lt;br /&gt;</p>
<p>&lt;/td&gt;</p>
<p>&lt;/tr&gt;</p>
<p>&lt;tr&gt;</p>
<p>&lt;td&gt;</p>
<p>&lt;h:panelGrid id=&#8221;detailtab&#8221;&gt;</p>
<p>Name: &lt;h:inputText value=&#8221;#{employeeActionBean.currentEmp.name}&#8221; /&gt; &lt;br/&gt;</p>
<p>Address:    &lt;h:inputText value=&#8221;#{employeeActionBean.currentEmp.address}&#8221; /&gt; &lt;br/&gt;</p>
<p>&lt;h:commandButton value=&#8221;Save&#8221; action=&#8221;#{employeeActionBean.saveUpdateAction}&#8221; /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;</p>
<p>&lt;h:commandButton value=&#8221;Cancel&#8221; action=&#8221;#{employeeActionBean.ClearAction}&#8221; /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;</p>
<p>&lt;h:commandButton value=&#8221;Delete&#8221; action=&#8221;#{employeeActionBean.DeleteAction}&#8221; /&gt;</p>
<p>&lt;/h:panelGrid&gt;</p>
<p>&lt;/td&gt;</p>
<p>&lt;/tr&gt;</p>
<p>&lt;/table&gt;</p>
<p>&lt;/h:form&gt;</p>
<p>&lt;/f:view&gt;</p>
<p>&lt;/body&gt;</p>
<p>&lt;/html&gt;</p></blockquote>
<p>Also download following 2 images which are used in the jsp file.</p>
<p><a href="http://www.skill-guru.com/blog/wp-content/uploads/2009/08/edit_icon.gif">http://www.skill-guru.com/blog/wp-content/uploads/2009/08/edit_icon.gif</a></p>
<p><a href="http://www.skill-guru.com/blog/wp-content/uploads/2009/08/checkmark.gif">http://www.skill-guru.com/blog/wp-content/uploads/2009/08/checkmark.gif</a></p>
<p>Now start the tomcat and run the jsf file you will see something like below:</p>
<p><a href="http://www.skill-guru.com/blog/wp-content/uploads/2009/08/tableResult.GIF"><img class="alignnone size-full wp-image-455" title="tableResult" src="http://www.skill-guru.com/blog/wp-content/uploads/2009/08/tableResult.GIF" alt="tableResult" width="532" height="649" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.skill-guru.com/blog/2009/08/20/edit-delete-and-clear-records-using-jsf-datatable/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
