1. PageBean,保存一次分页查询结果的基本信息:
package com.huey.dream.util;
import java.util.List;
/**
* 工具类,保存一次分页查询结果的基本信息
* @author huey2672
* @version 1.0
* @created 2014-8-11
* @param <T>
*/
public class PageBean<T> {
private int totalSize; // 总记录数
private int pageCount; // 总页数,通过计算得出
private int currentPageIndex; // 当前页码
private int pageSize; // 每页记录数
private List<T> dataList; // 当前页记录
public int getTotalSize() {
return totalSize;
}
public void setTotalSize(int totalSize) {
this.totalSize = totalSize <= 0 ? 0 : totalSize;
}
public int getPageCount() {
return pageCount;
}
public int getCurrentPageIndex() {
return currentPageIndex;
}
public void setCurrentPageIndex(int currentPageIndex) {
this.currentPageIndex = currentPageIndex <= 0 ? 0 : currentPageIndex;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize <= 0 ? 0 : pageSize;
}
public List<T> getDataList() {
return dataList;
}
public void setDataList(List<T> dataList) {
this.dataList = dataList;
}
public PageBean(int totalSize, int currentPageIndex, int pageSize,
List<T> dataList) {
super();
setTotalSize(totalSize);
setPageSize(pageSize);
setCurrentPageIndex(currentPageIndex);
setDataList(dataList);
this.pageCount = totalSize / pageSize + (totalSize % pageSize == 0 ? 0 : 1);
}
/**
* 打印PageBean的信息
*/
public void showInfo() {
System.out.println("TotalSize: " + totalSize);
System.out.println("PageCount: " + pageCount);
System.out.println("CurrentPageIndex: " + currentPageIndex);
System.out.println("PageSize: " + pageSize);
System.out.println("Data:");
for (T obj : dataList) {
System.out.println(obj);
}
}
}
2. 业务逻辑层接口StudentServ,分页查询返回一个PageBean对象实例:
package com.huey.dream.serv;
import com.huey.dream.entity.Student;
import com.huey.dream.util.PageBean;
/**
* 业务逻辑层接口StudentServ
* @author huey2672
* @version 1.0
* @created 2014-8-11
*/
public interface StudentServ {
/**
* 根据关键字分页查询学生记录
* @param keyword
* @param pageIndex
* @param pageSize
* @return
*/
public PageBean<Student> pagingSearchStudents(String keyword, int pageIndex, int pageSize);
}
3. zul页面index.zul:
<?page title="Index" contentType="text/html;charset=UTF-8"?>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver" ?>
<zk>
<window id="studentWindow" title="Index" border="normal" width="600px"
apply="${studentSearchController}" >
<hbox>
<textbox id="keywordBox" width="250px" />
<button id="searchButton" label="Search" image="/img/search.png" />
<label value="You are using: ${desktop.webApp.version}"/>
</hbox>
<listbox id="studentListbox" style="margin-top:10px" >
<listhead>
<listheader label="学号" />
<listheader label="姓名" />
<listheader label="生日" />
<listheader label="E-mail" />
</listhead>
<template name="model">
<listitem>
<listcell label="${each.no}" />
<listcell label="${each.name}" />
<listcell label="${each.birthday}" />
<listcell label="${each.email}" />
</listitem>
</template>
</listbox>
<!-- 分页组件 -->
<paging id="studentPaging" pageSize="5" />
</window>
</zk>
4. ZK组件控制器StudentSearchController:
package com.huey.dream.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Paging;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.event.PagingEvent;
import com.huey.dream.serv.StudentServ;
import com.huey.dream.util.PageBean;
import com.huey.dream.entity.Student;
/**
* 控制器层StudentSearchController
* @author huey2672
* @version 1.0
* @created 2014-8-11
*/
// 注入控制层Bean
@Controller("studentSearchController")
public class StudentSearchController extends SelectorComposer<Component> {
/**
*
*/
private static final long serialVersionUID = -8042049993596716142L;
@Wire
private Textbox keywordBox;
@Wire
private Listbox studentListbox;
@Wire
private Paging studentPaging;
private StudentServ studentServ;
@Autowired
public void setStudentServ(StudentServ studentServ) {
this.studentServ = studentServ;
}
/**
* 注册监听器,将该方法与id为searchButton的Button的onClick绑定
*/
@Listen("onClick=#searchButton")
public void search() {
// 关键字查询
final String keyword = keywordBox.getValue();
final int PAGE_SIZE = studentPaging.getPageSize();
PageBean<Student> studentPageBean = studentServ.pagingSearchStudents(keyword, 1, PAGE_SIZE);
List<Student> students = studentPageBean.getDataList();
studentListbox.setModel(new ListModelList<Student>(students));
// 设置总记录数
studentPaging.setTotalSize(studentPageBean.getTotalSize());
// 设置当前页页码
studentPaging.setActivePage(0);
// 为paging添加onPaging事件监听器
studentPaging.addEventListener("onPaging", new EventListener<Event>() {
public void onEvent(Event event) throws Exception {
PagingEvent pagingEvent = (PagingEvent) event;
// paging第一页的下标为0,而自定义工具类PageBean的第一页的下标为1
int pageIndex = pagingEvent.getActivePage() + 1;
studentListbox.setModel(new ListModelList<Student>(
studentServ.pagingSearchStudents(keyword, pageIndex, PAGE_SIZE)
.getDataList()
)
);
}
});
}
}