页面代码非常简单,没有复杂性。 检查“ index.xhtml”代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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">
<h:head>
</h:head>
<h:body><f:view><h:form><p:dataTable id="lazyDataTable" value="#{playerMB.allPlayers}" var="player" paginator="true" rows="10"selection="#{playerMB.player}" selectionMode="single"paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"rowsPerPageTemplate="5,10,15" style="width: 80%;margin-left: 10%;margin-right: 10%;"><p:ajax event="rowSelect" update=":playerDialogForm" oncomplete="playerDetails.show()" /><p:column><f:facet name="header">Name</f:facet><h:outputText value="#{player.name}" /></p:column><p:column><f:facet name="header">Age</f:facet><h:outputText value="#{player.age}" /></p:column></p:dataTable></h:form><p:dialog widgetVar="playerDetails" header="Player" modal="true"><h:form id="playerDialogForm"><h:panelGrid columns="2"><h:outputText value="Id: " /><h:outputText value="#{playerMB.player.id}" /><h:outputText value="Name: " /><h:outputText value="#{playerMB.player.name}" /><h:outputText value="Age: " /><h:outputText value="#{playerMB.player.age}" /></h:panelGrid></h:form></p:dialog></f:view>
</h:body>
</html>
我们得到了一个懒惰的数据表,它将在对话框中显示选定的值。
在我们的Managed Bean中,我们有比页面更简单的代码:
package com.mb;import java.io.Serializable;import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;import org.primefaces.model.LazyDataModel;import com.model.Player;@ViewScoped
@ManagedBean
public class PlayerMB implements Serializable {private static final long serialVersionUID = 1L;private LazyDataModel<Player> players = null;private Player player;public LazyDataModel<Player> getAllPlayers() {if (players == null) {players = new PlayerLazyList();}return players;}public Player getPlayer() {if(player == null){player = new Player();}return player;}public void setPlayer(Player player) {this.player = player;}
}
我们对播放器实体进行了获取/设置,对LazyDataModel类型的对象进行了获取。
在下面检查PlayerLazyList代码的实现
package com.mb;import java.util.List;
import java.util.Map;import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;import com.connection.MyTransaction;
import com.dao.PlayerDAO;
import com.model.Player;public class PlayerLazyList extends LazyDataModel<Player> {private static final long serialVersionUID = 1L;private List<Player> players;private MyTransaction transaction;private PlayerDAO playerDAO;@Overridepublic List<Player> load(int startingAt, int maxPerPage, String sortField, SortOrder sortOrder, Map<String, String> filters) {try {try {transaction = MyTransaction.getNewTransaction();playerDAO = new PlayerDAO(transaction);transaction.begin();// with datatable pagination limitsplayers = playerDAO.findPlayers(startingAt, maxPerPage);// If there is no player created yet, we will create 100!!if (players == null || players.isEmpty()) {playerDAO.create100Players();// we will do the research again to get the created playersplayers = playerDAO.findPlayers(startingAt, maxPerPage);}} finally {transaction.commit();}} catch (Exception e) {e.printStackTrace();}// set the total of playersif(getRowCount() <= 0){setRowCount(playerDAO.countPlayersTotal());}// set the page dizesetPageSize(maxPerPage);return players;}@Overridepublic Object getRowKey(Player player) {return player.getId();}@Overridepublic Player getRowData(String playerId) {Integer id = Integer.valueOf(playerId);for (Player player : players) {if(id.equals(player.getId())){return player;}}return null;}
}
关于上面的代码:
- 加载方法:每次启动分页时,Primefaces都会调用此方法。 它将具有有效值的所有参数; 使用这些参数,您将能够在数据库中进行查询,仅获取所需的数据。 如果要按字段对查询排序,则可以使用sortField属性,该属性将具有列datatable值(如果用户不进行排序,则为null); sortOrder将指示用户是否要递增或递减。
- getRowKey方法:此方法向每行返回一个ID,Primefaces将在需要时调用此方法。
- getRowData方法:将在数据表中返回选定的Player。
- 首次运行此应用程序时,它将在数据库中保留100个播放器。 在实际应用中,这不是必需的。
最后一个配置需要添加到“ web.xml”文件中:
<persistence-context-ref><persistence-context-ref-name>JSFPU</persistence-context-ref-name><persistence-unit-name>JSFPU</persistence-unit-name>
</persistence-context-ref>
我们将使用此配置来执行JNDI查找。
运行我们的应用程序
现在我们只需要启动应用程序。
要访问该应用程序,您可以使用链接:
http:// localhost:8080 / DatatableLazyPrimefaces /
单击此处下载此帖子的源代码。
参考: uaiHebert博客上我们JCG合作伙伴 Hebert Coelho的懒惰JSF数据表分页(Primefaces) 。
翻译自: https://www.javacodegeeks.com/2012/04/lazy-jsf-primefaces-datatable_11.html