我正在使用PrimceFaces并想要渲染一个表,其列数是动态确定的。我在这里找到了一个例子:
https://www.primefaces.org/showcase/ui/data/datatable/columns.xhtml
我认为我正确地实现了它,但表没有呈现。实际上什么都没有呈现。我调试了程序并确定:
CinemaSeatViewBean.getSeatsForShowList()
被调用,但方法
CinemaSeatViewBean.getDataTableColumns()
完全没有被召唤。
在我的代码下面:
1.)管理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.)小脸:
<!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.)DataTableColumn类:
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.)我希望我正确启用了DEVELOPMENT_MODE:
<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>
除非有拼写错误,
<p:columns value="#{dataTableView.dataTableColumns}" var="column" sortBy="#{seatForShow[column.property]}">
应该
<p:columns value="#{cinemaSeatViewBean.dataTableColumns}" var="column" sortBy="#{seatForShow[column.property]}">
但是你的IDE应该警告你这个并且我也期望一个运行时错误(除非bean实际存在于你的项目中并且在getter中没有做任何事情)。