hi ranchers,
iam newbie to
JSF and have some issue facing regarding h:table. My requriment is that i need to generate a table in which the column data comes from different objects. Please find below the sample program that i coded. Iam not sure if this the right approach. Please help
My
JSP:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<f:view>
<h:dataTable id="AG"
var="item"
value="#{tableController.dataLis}"
cellspacing="0"
styleClass="coable"
rowClasses="topRow"
summary="This table visually organizes information and is used for layout only">
<h:column>
<f:facet name="header"><h:outputText value ="Name" /></f:facet>
<h:outputText value="#{item.emp.name}"/>
</h:column>
<h:column>
<f:facet name="header"><h:outputText value ="Expdate" /></f:facet>
<h:outputText value="#{item.product.expDate}"/>
</h:column>
</h:dataTable>
</f:view>
</body>
</html>
My backing bean:
public class TableController{
private TableData tabledata = new TableData();
private ArrayList<TableData> dataLis = new ArrayList<TableData>();
private ArrayList rawlist = new ArrayList();
public TableController() {
this.populatedatatable();
}
public void populatedatatable(){
tabledata.emp.setName("Vishnu");
tabledata.product.setExpDate("10103456");
rawlist.add(tabledata.emp.getName());
rawlist.add(tabledata.product.getExpDate());
for(int i =0;i<rawlist.size();i++){
System.out.println(rawlist.get(i));
}
dataLis.addAll(rawlist);
}
public ArrayList<TableData> getDataLis(){
return this.dataLis;
}
public void setDataLis(ArrayList<TableData> dataLis) {
this.dataLis = dataLis;
}
public TableData getTabledata(){
return this.tabledata;
}
public void setTabledata(TableData tabledata) {
this.tabledata = tabledata;
}
}
My objects:
public class TableData {
public Employee emp=new Employee();
public Product product= new Product();
public TableData(){
}
public Employee getEmp(){
return this.emp;
}
public void setEmp(Employee emp){
this.emp=emp;
}
public Product getProduct(){
return this.product;
}
public void setProduct(Product product){
this.product=product;
}
}
public class Employee {
private int id;
private
String name;
public Employee(){}
public Employee (int id,String name){
this.id = id;
this.name= name;
}
public void setId(int id){
this.id =id;
}
public int getId(){
return this.id;
}
public void setName(String name){
this.name =name;
}
public String getName(){
return this.name;
}
}
faces-config.xml:
<managed-bean>
<managed-bean-name>tableController</managed-bean-name>
<managed-bean-class>com.controller.TableController</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>tabledata</property-name>
<property-class>com.model.TableData</property-class>
<value>#{tabledata}</value>
</managed-property>
</managed-bean>
<managed-bean>
<managed-bean-name>tabledata</managed-bean-name>
<managed-bean-class>com.model.TableData</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>emp</property-name>
<property-class>com.model.Employee</property-class>
<value>#{emp}</value>
</managed-property>
<managed-property>
<property-name>product</property-name>
<property-class>com.model.Product</property-class>
<value>#{product}</value>
</managed-property>
Now the questions i have are
1) The data don't show up in the table. Please let me know if this the right approach or is there any other method to do this task.
2) Do i need to create separate tabledata.java bean or can i have that code in tablecontroller.
java class itself?
3) Do i need to declare both tabledata.java and tablecontroller.java as managed beans?
4) Do i need to specify properties for each managed bean?
5) Do i need to have <jsp:useBean> tag for tablecontroller inspite of declaring it as managed bean in JSP?
I really appreciate your help and eagerly waiting for your reply.
Thanks