2 Replies Latest reply on Jul 6, 2007 12:19 AM by Pakhi Sharma

    Problem with filling a dataTable

    Kirsten Grüne Newbie

      Hello!

      I have got a problem with filling my dataTable with the right issues. Could pleeeaaassse someone help me with this?

      The code for my jsp-page, where the result is shown, is:

      <h:dataTable id="data" style="width: 100%"
      styleClass...
      var="what do I have to fill in???" value="#{suggestionBox.result}">
      <h:column>
      <f:facet name="header">
      <h:outputText value="title" />
      </f:facet>
      <h:outputText value= "#{suggestionBox.result}" />
      </h:column>
      <h:column>
      <f:facet name="header">
      <h:outputText value="id" />
      </f:facet>
      <h:outputText value= "#{suggestionBox.result}" />
      </h:column>
      <h:column>
      <f:facet name="header">
      <h:outputText value="score" />
      </f:facet>
      <h:outputText value= "#{suggestionBox.result}" />
      </h:column>
      <h:column>
      <f:facet name="header">
      <h:outputText value="source" />
      </f:facet>
      <h:outputText value= "#{suggestionBox.result}" />
      </h:column>
      </h:dataTable>

      I have , ,... -tags around all this, of course.

      The code of my bean is:

      public class SG implemets Serializable...

      private String...

      public List autocomplete(Object event) {
      List v = new Vector() ... for my suggestionBox!!

      private List executeSuggestQuery(String query)...for the datable which is shown in the suggestion Box!!

      public String Testausgabe() {
      ArrayList list;
      try {
      list = search("\""+property+"\"", medline_index, medline_fields);
      result = "";
      for (int i=1; i<list.size(); i++)
      {

      result += i+" "+list.get(i).toString()+"\n";
      }
      catch...
      }
      return "success";

      then the method search:

      public ArrayList search(String q, ...)

      ...

      return list;

      public String getResult()...

      public String getData(){...

      ...

      I am working with Tomcat 6.
      The String that I put in the suggestionBox is searched in an index and I get the result on my jsp-page. Know, of course, with my jsp-code, the whole result is listed in each column. But what do I have to do, so that only the title is listed in the column title and only the id is listed in the column id,...???

      I am going mad with this. Could please someone help me with this!

      Thank you!

        • 1. Re: Problem with filling a dataTable
          Pakhi Sharma Newbie

          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 Sharma Newbie

            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.