0
votes

I am using PrimceFaces and want to render a table, whose number of columns are determined dynamically. I found an example here:

https://www.primefaces.org/showcase/ui/data/datatable/columns.xhtml

and I think I implement it correctly, but the table is not rendered. Actually nothing is rendered at all. I debugged the programm and ascertained that:

CinemaSeatViewBean.getSeatsForShowList() 

is getting called, but the method

CinemaSeatViewBean.getDataTableColumns()

is not called at all.

Below my code:

1.) The Managed Bean:

@ManagedBean
@ViewScoped
public class CinemaSeatViewBean implements Serializable {

    private static final long serialVersionUID = 1L;


    private List<SeatForShow> seatsForShowList = new ArrayList<SeatForShow>();
    private List<DataTableColumn> dataTableColumns = new ArrayList<DataTableColumn>();

    @PostConstruct
    public void init() {
        // add SeatForShows
        ExternalContext extContext = FacesContext.getCurrentInstance()
                .getExternalContext();

        Flash flash = extContext.getFlash();
        FilmShow selectedFilmShow = (FilmShow)flash.get(FilmAndCinemaChooserBean.SELECTED_FILM_SHOW);

        int noOfColumns = selectedFilmShow.getNumberOfColumns();

        for (int i = 0; i < noOfColumns; i ++){
            // prepare dynamic columns
            dataTableColumns.add(new DataTableColumn("Seat " + (i+1), "seatNo"));
        }

        for ( SeatForShow seatForShow: selectedFilmShow.getSeatsForShow()){
            seatsForShowList.add(seatForShow);
        }
    }

    public void setDataTableColumns(List<DataTableColumn> dataTableColumns) {
        this.dataTableColumns = dataTableColumns;
    }

    public List<DataTableColumn> getDataTableColumns() {
        return dataTableColumns;
    }

    public List<SeatForShow> getSeatsForShowList() {
        return seatsForShowList;
    }


    public void setSeatsForShowList(List<SeatForShow> seatsForShowList) {
        this.seatsForShowList = seatsForShowList;
    }
}

2.) The facelet:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui"
    xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
</h:head>
<h:body>
    <h:form id="seatsFormId">
        <p:dataTable id="seatsTableId" value="#{cinemaSeatViewBean.seatsForShowList}" var="seatForShow">
            <p:columns value="#{dataTableView.dataTableColumns}" var="column" sortBy="#{seatForShow[column.property]}">
                 <f:facet name="header">
                     <h:outputText value="#{column.header}" />
                 </f:facet>
                 <h:outputText value="#{seatForShow[column.property]}" />                      
            </p:columns>
        </p:dataTable>
    </h:form>
</h:body>
</html>

3.) The DataTableColumn class:

public class DataTableColumn implements Serializable{

    private static final long serialVersionUID = 1L;

    private String header;
    private String property;

    public DataTableColumn(String header, String property) {
        this.header = header;
        this.property = property;
    }

    public String getHeader() {
        return header;
    }

    public String getProperty() {
        return property;
    }
}

4.) I hope I enabled DEVELOPMENT_MODE correctly:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

<context-param>
    <param-name>primefaces.CLIENT_SIDE_VALIDATION</param-name>
    <param-value>true</param-value>
</context-param>
<context-param>
    <param-name>facelets.DEVELOPMENT</param-name>
    <param-value>true</param-value>
</context-param>

<context-param>
    <param-name>facelets.REFRESH_PERIOD</param-name>
    <param-value>0</param-value>
</context-param>

<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<!-- Map these files with JSF -->
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

<!-- Welcome page -->
<welcome-file-list>
    <welcome-file>selectFilmCinema.xhtml</welcome-file>
</welcome-file-list>

1
Try running your application in development mode. Most likely you'll get a hint then of the error. - Kukeltje
@Kukeltje I enabled the development mode, but nothing gets printed in the console as well - Alex Mi
No 'bean resolved to null' or something? Are you sure this is your minimal reproducible example? Not some other code in your project? And what do you see if you add a static column too? And a plain primeFaces p:inputText is shown? - Kukeltje
@Kukeltje: I would like to hope that this is my minimal, complete example. No, no message such as bean resolved to null. Yes, <p:inputText is shown correctly. I am not quite sure how cann I add a static column in this example. Could you please help? - Alex Mi
adding a p:column inside the datatable with fixed content... and sure you have value="#{dataTableView.dataTableColumns}" ? I would expect your IDE to give an error here and runtime too... bean name is not right - Kukeltje

1 Answers

2
votes

Unless there is a typo,

<p:columns value="#{dataTableView.dataTableColumns}" var="column" sortBy="#{seatForShow[column.property]}">

Should be

<p:columns value="#{cinemaSeatViewBean.dataTableColumns}" var="column" sortBy="#{seatForShow[column.property]}">

But your IDE should warn you for this and I expected a runtime error too (unlesss the bean actually exists in your project and does not do anything in the getter).