Iam using the sun 's core jsf tags <h:dataTable>. Here below is my code if u get confused with my code please leave it and give the solution for bindings.
[u]view page[/u]
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<f:view>
<h:form>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h:dataTable
binding="#{MultipleDataGrids.data}"
value="#{MultipleDataGrids.results}"
var="outerItem"
>
<h:column>
<h:commandLink action="#{MultipleDataGrids.action}">
<h:outputText value="#{outerItem.catName}" />
</h:commandLink>
<h:dataTable
binding="#{MultipleDataGrids.data2}"
value="#{outerItem.subCatList}"
var="innerItem"
>
<h:column> </h:column>
<h:column>
<h:commandLink action="#{MultipleDataGrids.action2}">
<h:outputText value="#{innerItem.catName}" />
</h:commandLink>
</h:column>
</h:dataTable>
</h:column>
</h:dataTable>
</body>
</h:form>
</f:view>
</html>
[u]Backing Bean[/u]
/*
* MultipleDataGrids.java
*
* Created on May 3, 2007, 5:59 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.gd.util;
import java.util.List;
import java.util.ArrayList;
import java.sql.*;
import com.gd.Category;
import javax.faces.component.UIData;
import javax.faces.context.FacesContext;
/**
*
* @author OM SAI
*/
public class MultipleDataGrids {
List results;
UIData data;
UIData data2;
public void setData2(UIData data2) {
this.data2 = data2;
}
public UIData getData2() {
return data2;
}
public void setData(UIData data) {
this.data = data;
}
public UIData getData() {
return data;
}
public List getResults() {
results=new ArrayList();
Connection con=new data.DBConnect().init();
try {
Statement request=con.createStatement();
Statement request2=con.createStatement();
ResultSet rs1,rs2;
rs1=request.executeQuery("Select * from catalog WHERE parentid='0' order by catalogname");
while(rs1.next()) {
Category temp=new Category();
temp.setCatName(rs1.getString("catalogname"));
temp.setCatId(rs1.getString("catalogid"));
temp.setSubCatList(new ArrayList());
rs2=request2.executeQuery("select * from catalog where parentid='"+temp.getCatId()+"' order by catalogname");
while (rs2.next()) {
Category ntemp=new Category();
ntemp.setCatId(rs2.getString("catalogid"));
ntemp.setCatName(rs2.getString("catalogname"));
temp.getSubCatList().add(ntemp);
}
results.add(temp);
}
con.close();
} catch (Exception e) {
System.out.println(e);
}
return results;
}
public MultipleDataGrids() {
}
public String action() {
Category selected = (Category) this.getData().getRowData();
System.out.println(selected.getCatName());
FacesContext ctx = FacesContext.getCurrentInstance();
ctx.getExternalContext().getSessionMap().put("NewsBeanMore", selected);
return "morenews";
}
public String action2() {
return "morens";
}
}
[u]FacesConfig.xml
[/u]
<managed-bean>
<managed-bean-name>MultipleDataGrids</managed-bean-name>
<managed-bean-class>com.gd.util.MultipleDataGrids</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
You can't bind the inner datatable to one property of the backing bean, because it expects one unique component. There are 3 solutions:
1) move the binding to, let's say #{outerItem.data2}.
2) Leave the binding away as you seem not to use it.
3) Crawl in the UIViewRoot for the dataTable component.
Your code looks nice, but I'd refractor the JDBC part to an DAO class.
Oh, and change the DOCTYPE declaration from HTML to XHTML ;)