现象:
在开发群里看到不少人在找ADF的分页,虽然11gr2有自带分页,但之前的版本没有,在 一定程度上不是很方便,本文基于迭代器Iterator做了一下例子
源码:
1.managedBean部分
package view.backing;
import javax.faces.event.ActionEvent;
import model.vo.TablePageModelImpl;
import oracle.jbo.ViewObject;
import view.utils.ADFUtils;
public class BusinessBean {
private int totalRows; //表中所有行数
private int firstRecode; //每个记录页第一行
private int currentPageNo = 1; //当前页数
private int totalPage; //基表被分成的所有页数
public BusinessBean() {
//初始化 获取所有行数
TablePageModelImpl am =
(TablePageModelImpl)ADFUtils.getApplicationModuleForDataControl("TablePageModelDataControl");
ViewObject vo = am.getAdT13UserOpTypeVO();
totalRows = (int)vo.getEstimatedRowCount();
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public int getTotalRows() {
return totalRows;
}
public void setFirstRecode(int firstRecode) {
this.firstRecode = firstRecode;
}
public int getFirstRecode() {
firstRecode = (this.getCurrentPageNo() - 1) * 10 + 1;
return firstRecode;
}
public void setCurrentPageNo(int currentPageNo) {
this.currentPageNo = currentPageNo;
}
public int getCurrentPageNo() {
return currentPageNo;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getTotalPage() {
totalPage =
(this.totalRows % 10 == 0) ? (this.totalRows / 10) : ((this.totalRows /
10) +
1);
return totalPage;
}
/**
*首页
* @param actionEvent
*/
public void first(ActionEvent actionEvent) {
System.out.println(totalRows);
this.setCurrentPageNo(1);
this.getFirstRecode();
}
/**
*下一页
* @param actionEvent
*/
public void next(ActionEvent actionEvent) {
if (this.getCurrentPageNo() < this.getTotalPage()) {
this.setCurrentPageNo(this.getCurrentPageNo() + 1);
this.getFirstRecode();
}
}
/**
*上一页
* @param actionEvent
*/
public void previous(ActionEvent actionEvent) {
if (this.getCurrentPageNo() > 1) {
this.setCurrentPageNo(this.getCurrentPageNo() - 1);
this.getFirstRecode();
}
}
/**
*尾页
* @param actionEvent
*/
public void last(ActionEvent actionEvent) {
this.setCurrentPageNo(this.getTotalPage());
this.getFirstRecode();
}
/**
*自选页数
* @param actionEvent
*/
public void go(ActionEvent actionEvent) {
this.setCurrentPageNo(this.getCurrentPageNo());
this.getFirstRecode();
}
}
2.页面代码
<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:af="http://xmlns.oracle.com/adf/faces/rich"
xmlns:trh="http://myfaces.apache.org/trinidad/html">
<jsp:directive.page contentType="text/html;charset=UTF-8"/>
<f:view>
<af:document id="d1">
<af:messages id="m1"/>
<af:form id="f1">
<af:panelGroupLayout id="pgl1" halign="center" layout="vertical"
styleClass="AFStretchWidth"
inlineStyle="width:900px;">
<af:outputText value="ADF Table分页实例" id="ot4"
inlineStyle="font-size:large; font-weight:bold;"/>
<trh:tableLayout id="tl1" borderWidth="1" styleClass="AFStretchWidth"
width="800px">
<trh:rowLayout id="rl2" inlineStyle="height:30px">
<trh:cellFormat id="cf2" halign="center">
<af:outputText value="OpTypeId" id="ot5"
inlineStyle="font-size:x-small; font-weight:bold;"/>
</trh:cellFormat>
<trh:cellFormat id="cellFormat4" halign="center">
<af:outputText value="OpTypeCode" id="outputText4"
inlineStyle="font-size:x-small; font-weight:bold;"/>
</trh:cellFormat>
<trh:cellFormat id="cellFormat5" halign="center">
<af:outputText value="OpTypeDesc" id="outputText5"
inlineStyle="font-size:x-small; font-weight:bold;"/>
</trh:cellFormat>
<trh:cellFormat id="cellFormat6" halign="center">
<af:outputText value="OpTypeGroup" id="outputText6"
inlineStyle="font-size:x-small; font-weight:bold;"/>
</trh:cellFormat>
</trh:rowLayout>
<af:iterator id="i1"
value="#{bindings.AdT13UserOpTypeVO.collectionModel}"
var="table" rows="10"
first="#{pageFlowScope.business.firstRecode}">
<trh:rowLayout id="rl1" width="600px" inlineStyle="height:40px">
<trh:cellFormat id="cf1" width="100px" halign="center">
<af:outputText value="#{table.OpTypeId}" id="ot1"/>
</trh:cellFormat>
<trh:cellFormat id="cellFormat1" width="350px" halign="center">
<af:outputText value="#{table.OpTypeCode}" id="outputText1"/>
</trh:cellFormat>
<trh:cellFormat id="cellFormat2" width="250px" halign="center">
<af:outputText value="#{table.OpTypeDesc}" id="outputText2"/>
</trh:cellFormat>
<trh:cellFormat id="cellFormat3" width="100px" halign="center">
<af:outputText value="#{table.OpTypeGroup}" id="outputText3"/>
</trh:cellFormat>
</trh:rowLayout>
</af:iterator>
</trh:tableLayout>
<af:panelGroupLayout id="pgl2" layout="horizontal" halign="center">
<af:commandButton text="首页" id="cb1"
actionListener="#{pageFlowScope.business.first}"/>
<af:commandButton text="下一页" id="cb2"
actionListener="#{pageFlowScope.business.next}"/>
<af:subform id="s1" defaultCommand="::cl1" default="true">
<af:inputText id="it1" contentStyle="width:30px" autoSubmit="true"
value="#{pageFlowScope.business.currentPageNo}"/>
</af:subform>
<af:commandLink text="跳转" id="cl1"
actionListener="#{pageFlowScope.business.go}"/>
<af:commandButton text="上一页" id="cb3"
actionListener="#{pageFlowScope.business.previous}"/>
<af:commandButton text="尾页" id="cb4"
actionListener="#{pageFlowScope.business.last}"/>
<af:outputText value="#{pageFlowScope.business.currentPageNo}/" id="ot2"/>
<af:outputText value="#{pageFlowScope.business.totalPage}" id="ot3"/>
</af:panelGroupLayout>
</af:panelGroupLayout>
</af:form>
</af:document>
</f:view>
</jsp:root>
3.效果图
结束
分页的方法还有很多,仅供参考