转自: http://blog.csdn.net/luyushuang/article/details/6756459
当页面需要显示的数据量比较大的时候,可以使用分页来简化用户的操作。但是在ADF 11g中,af:table并没有默认的分页功能,我们可以custom出JSPX页面的分页逻辑。
本例子使用的表是HR Sechema中的Employees。
2011/11/25 卢玉双 追加:
类似的实现方式,可以使用af:iterator,Table数据取自ADF BC的VO,也能够实现分页功能。
主要使用af:iterator这个tag,页面中数据的展示如同af:table,并且af:iterator可以基于table绑定进行循环显示数据集合。
1,基于Employees表创建entities,然后创建一个stateless session bean,以及data control,代码片段如下:
- public List<Employees> employeesByLimit(int firstRow, int maxRow) {
- String queryString = "select * from Employees order by employee_id ASC";
- return em.createNativeQuery(queryString,
- Employees.class).setMaxResults(maxRow).setFirstResult(firstRow).getResultList();
- }
- /**
- * Returns total amount of rows in table.
- * @return Total amount of rows in table.
- */
- public int employeesTotalRows() {
- String queryString = "select * from Employees order by employee_id ASC";
- Query query = em.createNativeQuery(queryString);
- List results = query.getResultList();
- return results.size();
- }
2,在ViewController层创建JSPX页面CustomPagination.jspx
3,创建页面对应的sessionScope级别的managed bean
代码片段:
- private int firstRow = 0;
- private int rowsPerPage = 10;
- private int totalRows;
- private int totalPages;
- private int currentPage = 1;
- public CustomPagination() {
- this.loadList();
- }
- public void loadList() {
- /**
- * Returns total amount of rows in table.
- * @return Total amount of rows in table.
- */
- BindingContainer bindings = BindingContext.getCurrent().getCurrentBindingsEntry();
- AttributeBinding attr = (AttributeBinding)bindings.getControlBinding("EmployeesTotalRowCount");
- String val = attr.getInputValue().toString();
- int rows = Integer.parseInt(val);
- this.setTotalRows(rows);
- double val1 = ((double)this.getTotalRows() / this.getRowsPerPage());
- int totalPagesCal = (int)Math.ceil(val1);
- this.setTotalPages((totalPagesCal != 0) ? totalPagesCal : 1);
- }
- public void firstActionListener(ActionEvent actionEvent) {
- this.setCurrentPage(1);
- this.setFirstRow(0);
- }
- public void previousActionListener(ActionEvent actionEvent) {
- this.setCurrentPage(this.getCurrentPage() - 1);
- this.setFirstRow(this.getFirstRow() - this.getRowsPerPage());
- }
- public void nextActionListener(ActionEvent actionEvent) {
- this.setCurrentPage(this.getCurrentPage() + 1);
- this.setFirstRow(this.getFirstRow() + this.getRowsPerPage());
- }
- public void lastActionListener(ActionEvent actionEvent) {
- this.setCurrentPage(this.getTotalPages());
- this.setFirstRow(this.getTotalRows() -
- ((this.getTotalRows() % this.getRowsPerPage() != 0) ? this.getTotalRows() %
- this.getRowsPerPage() : this.getRowsPerPage()));
- }
- public boolean isBeforeDisabled() {
- return this.getFirstRow() == 0;
- }
- public boolean isAfterDisabled() {
- return this.getFirstRow() >= this.getTotalRows() - this.getRowsPerPage();
- }
- public void setFirstRow(int firstRow) {
- this.firstRow = firstRow;
- }
- public int getFirstRow() {
- return firstRow;
- }
- public void setRowsPerPage(int rowsPerPage) {
- this.rowsPerPage = rowsPerPage;
- }
- public int getRowsPerPage() {
- return rowsPerPage;
- }
- public void setTotalRows(int totalRows) {
- this.totalRows = totalRows;
- }
- public int getTotalRows() {
- return totalRows;
- }
- public void setTotalPages(int totalPages) {
- this.totalPages = totalPages;
- }
- public int getTotalPages() {
- return totalPages;
- }
- public void setCurrentPage(int currentPage) {
- this.currentPage = currentPage;
- }
- public int getCurrentPage() {
- return currentPage;
- }
4,打开CustomPagination页面的数据绑定
1)创建Action绑定
2)在variableIterator标签下添加下面代码
- <variable Type="int" Name="employeesTotalRows_Return" IsQueriable="false" IsUpdateable="0" DefaultValue="${bindings.employeesTotalRows.result}"/>
3)创建引用employeesTotalRows_Return的属性绑定
4)创建调用employeesByLimit的Action绑定
5)创建Tree绑定给af:iterator使用
6)创建invokeAction,在页面的prepareMode阶段调用employeesTotalRows Action
7)最终,页面绑定如下图
5,修改页面代码
1)表的Title部分
- <af:panelGroupLayout id="pgl9" layout="horizontal">
- <af:spacer width="10" height="10" id="s9"/>
- <af:panelGroupLayout id="pgl10" inlineStyle="width:75px;" layout="horizontal">
- <af:outputText value="Employeed Id" id="ot1" inlineStyle="font-weight:bold;"/>
- </af:panelGroupLayout>
- <af:spacer width="10" height="10" id="s7"/>
- <af:panelGroupLayout id="pgl7" inlineStyle="width:75px;" layout="horizontal">
- <af:outputText value="First Name" id="ot6" inlineStyle="font-weight:bold;"/>
- </af:panelGroupLayout>
- <af:spacer width="10" height="10" id="s10"/>
- <af:panelGroupLayout id="pgl11" inlineStyle="width:75px;" layout="horizontal">
- <af:outputText value="Last Name" id="ot4" inlineStyle="font-weight:bold;"/>
- </af:panelGroupLayout>
- <af:spacer width="10" height="10" id="s11"/>
- <af:panelGroupLayout id="pgl12" inlineStyle="width:75px;" layout="horizontal">
- <af:outputText value="Email" id="ot7" inlineStyle="font-weight:bold;"/>
- </af:panelGroupLayout>
- <af:spacer width="10" height="10" id="s12"/>
- <af:panelGroupLayout id="pgl15" inlineStyle="width:75px;" layout="horizontal">
- <af:outputText value="Salary" id="ot10" inlineStyle="font-weight:bold;"/>
- </af:panelGroupLayout>
- </af:panelGroupLayout>
2)表的数据
- <af:iterator id="i1" value="#{bindings.result.collectionModel}" var="row">
- <af:panelGroupLayout id="pgl2" layout="horizontal">
- <af:spacer width="10" height="10" id="s3"/>
- <af:panelGroupLayout id="pgl3" layout="horizontal" inlineStyle="width:75px;">
- <af:outputText value="#{row.employeeId}" id="ot8"/>
- </af:panelGroupLayout>
- <af:spacer width="10" height="10" id="s13"/>
- <af:panelGroupLayout id="pgl13" layout="horizontal" inlineStyle="width:75px;">
- <af:outputText value="#{row.firstName}" id="ot11"/>
- </af:panelGroupLayout>
- <af:spacer width="10" height="10" id="s4"/>
- <af:panelGroupLayout id="pgl4" layout="horizontal" inlineStyle="width:75px;">
- <af:outputText value="#{row.lastName}" id="ot9"/>
- </af:panelGroupLayout>
- <af:spacer width="10" height="10" id="s6"/>
- <af:panelGroupLayout id="pgl5" layout="horizontal" inlineStyle="width:75px;">
- <af:outputText value="#{row.email}" id="ot2"/>
- </af:panelGroupLayout>
- <af:spacer width="10" height="10" id="s8"/>
- <af:panelGroupLayout id="pgl8" inlineStyle="width:75px;" layout="horizontal">
- <af:outputText value="#{row.salary}" id="ot3"/>
- </af:panelGroupLayout>
- </af:panelGroupLayout>
- <af:spacer width="10" height="10" id="s1"/>
- </af:iterator>
3)控制按钮
- <af:panelGroupLayout id="pgl6">
- <af:commandButton text="First" id="cb1"
- actionListener="#{CustomPagination.firstActionListener}"
- partialTriggers="i1" disabled="#{CustomPagination.beforeDisabled}"/>
- <af:commandButton text="Prev" id="cb2"
- actionListener="#{CustomPagination.previousActionListener}"
- partialTriggers="i1" disabled="#{CustomPagination.beforeDisabled}"/>
- <af:commandButton text="Next" id="cb3"
- actionListener="#{CustomPagination.nextActionListener}"
- partialTriggers="i1" disabled="#{CustomPagination.afterDisabled}"/>
- <af:commandButton text="Last" id="cb4"
- actionListener="#{CustomPagination.lastActionListener}"
- partialTriggers="i1" disabled="#{CustomPagination.afterDisabled}"/>
- </af:panelGroupLayout>
6,运行代码
首页:
第三页:
尾页: