-
1. Re: Problem with filling a dataTable
pakhi Jul 6, 2007 12:18 AM (in response to puh)Hi puh..
You are using the wrong way of datatable.
Please refer my code below:
<h:dataTable id="table" var="row"
value="#{registerBean.books}"
border="1" cellpadding="5" style="">
<h:column style="">
<f:facet name="header">
<h:outputText value="ISBN"/>
</f:facet>
<h:outputText value="#{row.ISBN}"/>
</h:column>
<h:column style="">
<f:facet name="header">
<h:outputText value="Tiltes"/>
</f:facet>
<h:outputText value="#{row.title}"/>
</h:column>
</h:dataTable>
In this, registerBean.books is my List object fronm the bean. and var is used to point a row of the List object. I am returning a query result in the list, thus ISBN and Tile are object in the entity.
I hope the use of datatable must be clear.
Try this..
ALL the best -
2. Re: Problem with filling a dataTable
pakhi Jul 6, 2007 12:19 AM (in response to puh)Even this will improve ur understanding:
Using JSF with the SQL Tags of JSTL The JSF API defines an abstract data model for tables (javax.faces.model.DataModel), and provides implementations that wrap arrays, lists, JDBC result sets, the results from the <sql:query> tag of JSTL, and scalars. The JSF data models have methods that let you access the row data, but it is up to the concrete model implementations how the rows are represented. When using arrays or lists, each element is a row. When using result sets, each row is represented as a java.util.Map whose keys are the table columns. Since the row model is not restricted, it is the Web developer's responsibility to define how the columns are rendered in a JSF page using <h:column> tags that must be nested inside a <h:dataTable> element.
The web.xml file of the example application declares a data source with <resource-ref> and configures it as the default JSTL data source with a context parameter named javax.servlet.jsp.jstl.sql.dataSource. Another parameter (adminEmail) specifies an email address that will be compared (in list.jsp) with the email property of the subscriber that is logged in:
<web-app>
<context-param>
<param-name>adminEmail</param-name>
<param-value>admin@localhost</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.sql.dataSource</param-name>
<param-value>jdbc/OracleDS</param-value>
</context-param>
...
<resource-ref>
<res-ref-name>jdbc/OracleDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
...
</web-app>
Only the user whose email address is specified in web.xml can execute the list.jsp page. Therefore, you have to subscribe as admin@localhost, after deploying the application. When you log in using the adminEmail address, the login.jsp page forwards the request to list.jsp. This authentication mechanism is used in the example application in order to show how an action method, such as loginAction(), can return different outcomes (list or profile) depending on a programmable condition. A real application would use the standard HTTP-based or from-based authentication to verify the identity of an administrator.
The list.jsp page executes an SQL query with the JSTL tag, which creates a subscriberList variable that holds the result set. This is passed to <h:dataTable>, which iterates over the rows of the result set, invoking the tag body for each row. Each <h:column> tag contains a <h:outputText value="#{row...}"/> tag that renders the value of the current cell, and a facet that is rendered as part of the table's header:
<!-- list.jsp -->
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<c:if test="${subscriber == null || !subscriber.loggedIn}">
<c:redirect url="/login.faces"/>
</c:if>
<c:if test="${subscriber.email != initParam.adminEmail}">
<c:redirect url="/profile.faces"/>
</c:if>
<sql:query var="subscriberList" scope="request">
SELECT * FROM subscribers ORDER BY subscriberEmail
</sql:query>
<f:view>
...
<h:form id="list">
<h:dataTable id="table" var="row"
value="#{subscriberList}"
border="1" cellpadding="5">
<h:column>
<f:facet name="header">
<h:outputText value="#{labels.email}"/>
</f:facet>
<h:outputText value="#{row.subscriberEmail}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{labels.password}"/>
</f:facet>
<h:outputText value="#{row.subscriberPassword}"/>
</h:column>
...
</h:dataTable>
</h:form>
</f:view>
The <sql:query> tag uses the request scope in the list.jsp page because JSF doesn't support the JSP page scope. If you forget to specify a scope supported by JSF, the JSTL tag will use the default page scope, and the tags of the two tag libraries will not be able to communicate. Therefore, make sure that you use one of the common scopes (request, session or application) when using the tags of JSF and JSTL together.