2 Replies Latest reply on Jan 18, 2009 9:34 AM by Nick Belaevski

    Mapping DB Tables to DataTable

    Gabriel Gomez Newbie

      Hi Everyone.

      Could someone enlighten me on this. Please :(

      The idea is pretty simple. Create a rich:dataTable (either static or dynamically) to show information saved on tables saved inside a DB. I cant create an XHTML page for each DB table, so i'm trying to create a "generic" dataTable that can load any info I send it.

      At this point I’m able to retrieve any info from any table inside the DB, but have come across the problem of showing this information in a dataTable Object.

      1st approach
      Does rich faces have something like this?
      <ice:outputText value="#{value.getColumnValue(rowId,columnName)}"/>
      I've already tried this but no good :(. I know it says ice, but is the an equivalent in rich?

      2nd aproach
      I've tried binding and creating and HTMLDataTable/UIDataTable. Only problem here is adding rows. Headers (columns names) are added correctly and dynamically. DataTable is expanded or contracted depending on the number of columns I add on the
      Problem is how to add each row of data to the column?

      I'm using this as an example (taken from https://jira.jboss.org/jira/browse/RF-3364;jsessionid=9FEC6C30D3A56EA1A345E11CD49B1335?page=com.atlassian.jira.plugin.system.issuetabpanels%3Achangehistory-tabpanel)

       String[] columns = new String[] { "number", "name" };
       String[] valueExprs = new String[] { "#{data.number}", "#{data.name}" };
       FacesContext facesContext = FacesContext.getCurrentInstance();
       ELContext elContext = facesContext.getELContext();
       Application application = facesContext.getApplication();
       ExpressionFactory elFactory = facesContext.getApplication().getExpressionFactory();
       dataTable = new HtmlDataTable();
       for (int i = 0; i < columns.length; i++) {
       log.info("setting column[" + i + "]: " + columns);
       // header
       HtmlOutputText header = new HtmlOutputText();
       // content
       HtmlOutputText content = new HtmlOutputText();
       ValueExpression valueExpression = elFactory.createValueExpression(
       elContext, valueExprs, String.class);
       content.setValueExpression("value", valueExpression);
       // column
       HtmlColumn column = new HtmlColumn();
       column.setId("col" + i);
       return dataTable;

      since DB number of columns increase of decrease depending on the table i'm consulting I cant use something like "#{bean.colum1}","#{bean.colum2}", and so on... because i don’t know how many rows are coming.

      As a real life example of what I’m trying to do you can look at OracleXE web interface. When user selects a table, web interface loads table data into page expanding or contracting the "dataTable" where it been shown. That’s the sort of behaviour I’m trying to create

      Could someone point me in the right direction to do this using rich:dataTable? Either statically (using rich tags) or dynamically (using HTMLDataTable/UIDataTable)

      Thanks in advance :D

        • 1. Re: Mapping DB Tables to DataTable
          fabian maass Newbie


          im pretty new to jsf/rf myself, but i guess what you want to to is using a datamodel in your backing bean... something in the lines of:

          public DataModel initDataModel() {
           resultModel = new ListDataModel();
           List list = new YourDataTableDAO().findAll();
           return resultModel;

          please keep the jsf lifecycles in mind and dont put the database code directly in the getter, since it will be accessed a few times during the request.

          hope this helped a bit


          • 2. Re: Mapping DB Tables to DataTable
            Nick Belaevski Master


            I would suggest to have a look at javax.faces.model.ResultDataModel and javax.faces.model.ResultSetDataModel. Both models contain enough information to build data table dynamically and both are supported by h:dataTable either as rich:dataTable.

            Another suggestion is to build adaptor class that will represent data for each row in indexed form.