Presenting a dynamically generated table using JSF core tags and JSTL.

Lets see!

Start with directory layout:

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/maven-v4_0_0.xsd">
 
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>biz.tugay</groupId>
    <artifactId>dynamic-table</artifactId>
    <packaging>war</packaging>
 
    <version>1.0-SNAPSHOT</version>
 
    <name>dynamic-table Maven Webapp</name>
    <url>http://www.tugay.biz</url>
 
    <dependencies>
        <dependency>
            <groupId>javax.faces</groupId>
            <artifactId>javax.faces-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.2.13</version>
        </dependency>
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>3.0.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
 
    <build>
        <finalName>dynamic-table</finalName>
        <plugins>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.2.1.v20140609</version>
                <configuration>
                    <scanIntervalSeconds>2</scanIntervalSeconds>
                    <webApp>
                        <contextPath>/</contextPath>
                    </webApp>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
</project>

web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                       http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
 
    <servlet>
        <servlet-name>FacesServlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>FacesServlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
 
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
 
</web-app>

Table.java, Column.java, Row.java and Cell.java
package biz.tugay.dynamictable;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * User: Koray Tugay (koray@tugay.biz)
 * Date: 7/22/2016
 * Time: 5:13 PM
 */
public class Table {
    private List<Column> columns = new ArrayList<Column>();
    private List<Row> rows = new ArrayList<Row>();
 
    public List<Column> getColumns() {
        return columns;
    }
 
    public List<Row> getRows() {
        return rows;
    }
}
 
package biz.tugay.dynamictable;
 
/**
 * User: Koray Tugay (koray@tugay.biz)
 * Date: 7/22/2016
 * Time: 5:12 PM
 */
public class Column {
}
 
package biz.tugay.dynamictable;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * User: Koray Tugay (koray@tugay.biz)
 * Date: 7/22/2016
 * Time: 5:13 PM
 */
public class Row {
 
    private List<Cell> cells = new ArrayList<Cell>();
 
    public List<Cell> getCells() {
        return cells;
    }
 
    public void setCells(List<Cell> cells) {
        this.cells = cells;
    }
}
 
package biz.tugay.dynamictable;
 
/**
 * User: Koray Tugay (koray@tugay.biz)
 * Date: 7/22/2016
 * Time: 5:13 PM
 */
public class Cell {
    private String value;
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
}

DynamicTableBacking.java
package biz.tugay;
 
import biz.tugay.dynamictable.*;
 
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import java.util.concurrent.ThreadLocalRandom;
 
/**
 * User: Koray Tugay (koray@tugay.biz)
 * Date: 7/22/2016
 * Time: 5:14 PM
 */
 
@ManagedBean
@RequestScoped
public class DynamicTableBacking {
 
    private Table table;
 
    @PostConstruct
    public void initialize() {
        final Table dynamicTable = new Table();
 
        // Generate random number of columns:
        final int numberOfColumns = ThreadLocalRandom.current().nextInt(3, 7);
        for (int i = 0; i < numberOfColumns; i++) {
            final Column column = new Column();
            dynamicTable.getColumns().add(column);
        }
 
        // Generate random number of rows:
        final int numberOfRows = ThreadLocalRandom.current().nextInt(5, 10);
        for (int i = 0; i < numberOfRows; i++) {
            final Row row = new Row();
            // Assign random values to the cells of this row:
            for (int k = 0; k < numberOfColumns; k++) {
                final Cell cell = new Cell();
                cell.setValue(String.valueOf(ThreadLocalRandom.current().nextInt(50, 1000)));
                row.getCells().add(cell);
            }
            dynamicTable.getRows().add(row);
        }
        table = dynamicTable;
    }
 
    public Table getTable() {
        return table;
    }
 
    public void setTable(Table table) {
        this.table = table;
    }
}

and finally index.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!--
    User: Koray Tugay (koray@tugay.biz)
    Date: 7/22/2016
    Time: 5:12 PM
-->
 
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
    <title>Dynamic Table</title>
</h:head>
<h:body>
    <h:panelGrid columns="#{dynamicTableBacking.table.columns.size()}" border="1">
        <c:forEach items="#{dynamicTableBacking.table.rows}" var="row">
            <c:forEach items="#{row.cells}" var="cell">
                #{cell.value}
            </c:forEach>
        </c:forEach>
    </h:panelGrid>
</h:body>
</html>

Lets build our application:
C:\Users\Koray Tugay\Development\_Projects\dynamic-table>mvn clean install
[INFO] Scanning for projects...
[INFO] Building dynamic-table Maven Webapp 1.0-SNAPSHOT
[INFO] BUILD SUCCESS
[INFO] Total time: 1.988 s
[INFO] Finished at: 2016-07-22T17:53:44+03:00
[INFO] Final Memory: 15M/154M
 
C:\Users\Koray Tugay\Development\_Projects\dynamic-table>mvn jetty:start
[INFO] Scanning for projects...
[INFO] Building dynamic-table Maven Webapp 1.0-SNAPSHOT
[INFO] Logging initialized @1997ms
[INFO] Configuring Jetty for project: dynamic-table Maven Webapp
[INFO] webAppSourceDirectory not set. Trying src\main\webapp
[INFO] Reload Mechanic: automatic
[INFO] Context path = /
[INFO] Started Jetty Server
[INFO] Starting scanner at interval of 2 seconds.

Let's visit localhost and refresh the page a few times: