一,后台查询,分页封装
1.BaseQuery.java:
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
public class BaseQuery implements Serializable {
private Integer pageSize = 5;
private Integer currentPage = 1;
private Integer startRow = 0;
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
if(pageSize == null || pageSize <= 0) {
return;
}
this.pageSize = pageSize;
}
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
if(currentPage == null || currentPage <= 0) {
return;
}
this.currentPage = currentPage;
}
public Integer getStartRow() {
if(currentPage == null || currentPage < 0 || pageSize == null && pageSize < 0) {
return 0;
}
startRow = (currentPage - 1) * pageSize;
return startRow;
}
public Map<String, Object> bulid() {
Map<String, Object> params = new HashMap<String, Object>();
params.put("pageSize", this.pageSize);
params.put("currentPage", this.currentPage);
params.put("startRow", this.getStartRow());
return params;
}
}
2.查询类继承BaseQuery,以UserQuery.java为例。
import java.util.Map;
public class UserQuery extends BaseQuery {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map<String, Object> build() {
Map<String, Object> params = super.bulid();
params.put("name", name);
return params;
}
public String queryString() {
StringBuilder sb = new StringBuilder();
sb.append("name=");
sb.append(name == null ? "" : name);
return sb.toString();
}
}
3.QueryResult.java:
import java.io.Serializable;
import java.util.*;
/**
* 分页查询结果
* @param <T>
*/
public class QueryResult<T> implements Serializable {
private Integer totalPage;//总页数
private Integer amount;//总条数
private Collection<T> resultList = new ArrayList<T>();//当前页数据集合
private BaseQuery query;
public Integer getTotalPage() {
return totalPage;
}
public Integer getAmount() {
return amount;
}
public void setAmount(Integer amount) {
if (amount == null) {
return;
}
if (query == null) {
return;
}
int pageSize = query.getPageSize();
int mod = amount % pageSize;
int page = amount / pageSize;
totalPage = (mod == 0 ? page : page + 1);
this.amount = amount;
}
public Collection<T> getResultList() {
return resultList;
}
public void setResultList(Collection<T> resultList) {
this.resultList = resultList;
}
public BaseQuery getQuery() {
return query;
}
public void setQuery(BaseQuery query) {
this.query = query;
}
public Integer getNextPage() {
Integer nextPage = query.getCurrentPage() + 1;
return nextPage > totalPage ? -1 : nextPage;
}
public List<Integer> pageNumbers() {
if(totalPage == 0) {
return Collections.EMPTY_LIST;
}
List<Integer> pages = new ArrayList<Integer>();
for(int i = 1; i<= totalPage; i++) {
pages.add(i);
}
return pages;
}
//***************************utils*****************************//
public Map<String, Object> propertyMap() {
Map<String, Object> pm = new HashMap<String, Object>(5);
pm.put("amount", this.amount);
pm.put("totalPage", this.totalPage);
pm.put("resultList", this.resultList);
if (query != null) {
pm.putAll(query.build());
}
return pm;
}
}
二,前台用velocity的宏定义显示分页。
1.list_user.vm:
<div class="content">
<form action="list_user.jhtml" method="post">
<label>姓名</label>
<input type="text" name="name" value="$!{query.name}" />
<input type="submit" value="查询">
</form>
<table class="table1" width="100%" style="margin-top: 20px;">
<tr class="headline">
<td>借款人姓名</td>
</tr>
#foreach( $user in $!users )
<tr>
<td>$!user.name</td>
</tr>
#end
</table>
#pagination("list_user.jhtml?$!{query.queryString()}" $!queryResult)
</div>
2.macro.vm
#macro(pagination $targetUrl $queryResult)
<div id="kkpager"></div>
<script type="text/javascript" src="/system-web/lib/kkpager.min.js"></script>
<script type="text/javascript">
//init
$(function(){
//生成分页
//有些参数是可选的,比如lang,若不传有默认值
kkpager.generPageHtml({
//当前页码
pno : $!{query.currentPage},
//总页码
total : $!{queryResult.totalPage},
//总数据条数
totalRecords : $!{queryResult.amount},
//链接前部
hrefFormer : '$targetUrl',
//链接尾部
hrefLatter : '',
getLink : function(n){
if (n == 1){
return this.hrefFormer;
}
return this.hrefFormer + "&page="+n;
}
});
});
</script>
#end
3.使用velocity配置引入macro.vm
a. 在spring-velocity.xml配置中加入macroHelper:
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
<property name="exposeSpringMacroHelpers" value="true"/>
</bean>
b. 在velocity.properties中加入:velocimacro.library.autoreload=true
velocimacro.library=macro.vm