list) {
this.setCount(count);
this.setPageNo(pageNo);
this.pageSize = pageSize;
this.list = list;
}
/**
* 初始化参数
*/
public void initialize(){
//1
this.first = 1;
this.last = (int)(count / (this.pageSize < 1 ? 20 : this.pageSize) + first - 1);
if (this.count % this.pageSize != 0 || this.last == 0) {
this.last++;
}
if (this.last < this.first) {
this.last = this.first;
}
if (this.pageNo <= 1) {
this.pageNo = this.first;
this.firstPage=true;
}
if (this.pageNo >= this.last) {
this.pageNo = this.last;
this.lastPage=true;
}
if (this.pageNo < this.last - 1) {
this.next = this.pageNo + 1;
} else {
this.next = this.last;
}
if (this.pageNo > 1) {
this.prev = this.pageNo - 1;
} else {
this.prev = this.first;
}
//2
if (this.pageNo < this.first) {// 如果当前页小于首页
this.pageNo = this.first;
}
if (this.pageNo > this.last) {// 如果当前页大于尾页
this.pageNo = this.last;
}
}
/**
* 默认输出当前分页标签
* ${page}
*/
@Override
public String toString() {
if(list != null && list.isEmpty()) {
return " "+requestContext.getMessage("noneData")+"
";
}
StringBuilder sb = new StringBuilder();
if (pageNo == first) {// 如果是首页
sb.append("« "+requestContext.getMessage("previousPage")+" \n");
} else {
sb.append("« "+requestContext.getMessage("previousPage")+" \n");
}
int begin = pageNo - (length / 2);
if (begin < first) {
begin = first;
}
int end = begin + length - 1;
if (end >= last) {
end = last;
begin = end - length + 1;
if (begin < first) {
begin = first;
}
}
if (begin > first) {
int i = 0;
for (i = first; i < first + slider && i < begin; i++) {
sb.append(""
+ (i + 1 - first) + " \n");
}
if (i < begin) {
sb.append("... \n");
}
}
for (int i = begin; i <= end; i++) {
if (i == pageNo) {
sb.append("" + (i + 1 - first)
+ " \n");
} else {
sb.append(""
+ (i + 1 - first) + " \n");
}
}
if (last - end > slider) {
sb.append("... \n");
end = last - slider;
}
for (int i = end + 1; i <= last; i++) {
sb.append(""
+ (i + 1 - first) + " \n");
}
if (pageNo == last) {
sb.append(""+requestContext.getMessage("nextPage")+" » \n");
} else {
sb.append(""
+ ""+requestContext.getMessage("nextPage")+" » \n");
}
sb.append(""+requestContext.getMessage("current")+" ");
sb.append(" / ");
/*sb.append(pageNo+" / ");*/
/*sb.append(" "+requestContext.getMessage("page")+",");
sb.append(""+requestContext.getMessage("total")+" " + count + " "+requestContext.getMessage("count")+""+(message!=null?message:"")+" \n");
sb.insert(0,"\n");
sb.append("
");
// sb.insert(0,"\n").append("
\n");
Map cancelMap = validateCancelService();
if(cancelMap!=null) {
String requestName = (String)cancelMap.get("requestName");
String indexTable = (String)cancelMap.get("indexTable");
sb.append(" ");
}
return sb.toString();
}
//判断是否有requestName indexTableName
public Map validateCancelService() {
Map map = new HashMap();
if(list != null && !list.isEmpty()) {
Object obj = (Object) list.get(0);
Class> clazz = obj.getClass();
tab: for (; clazz != Object.class; clazz = clazz.getSuperclass()) {//向上循环 遍历父类
Field[] field = clazz.getDeclaredFields();
for (Field f : field) {
String name = f.getName();
if(name!=null&&name.equals("requestName")) {
Object v = invokeMethod(obj, name);
if(v!=null) {
map.put("requestName", v);
}
}
if(name!=null&&name.equals("indexTable")) {
Object v = invokeMethod(obj, name);
if(v!=null) {
map.put("indexTable", v);
}
}
if(map.containsKey("requestName")&&map.containsKey("indexTable")) {
break tab;
}
}
}
}
return map;
}
/**
*
* 执行某个Field的getField方法
*
* @param owner 类
* @param fieldName 类的属性名称
* @param args 参数,默认为null
* @return
*/
private Object invokeMethod(Object owner, String fieldName)
{
Class extends Object> ownerClass = owner.getClass();
//fieldName -> FieldName
String methodName = fieldName.substring(0, 1).toUpperCase()+ fieldName.substring(1);
Method method = null;
//invoke getMethod
try
{
method = ownerClass.getMethod("get" + methodName);
if (method!=null) {
return method.invoke(owner);
}else {
return "";
}
}
catch (Exception e)
{
return "";
}
}
/**
* 获取分页HTML代码
* @return
*/
@JsonIgnore
public String getHtml(){
return toString();
}
// public static void main(String[] args) {
// Page p = new Page(3, 3);
// System.out.println(p);
// System.out.println("首页:"+p.getFirst());
// System.out.println("尾页:"+p.getLast());
// System.out.println("上页:"+p.getPrev());
// System.out.println("下页:"+p.getNext());
// }
/**
* 获取设置总数
* @return
*/
public long getCount() {
return count;
}
/**
* 设置数据总数
* @param count
*/
public void setCount(long count) {
this.count = count;
if (pageSize >= count){
pageNo = 1;
}
}
/**
* 获取当前页码
* @return
*/
public int getPageNo() {
return pageNo;
}
/**
* 设置当前页码
* @param pageNo
*/
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
/**
* 获取页面大小
* @return
*/
public int getPageSize() {
return pageSize;
}
/**
* 设置页面大小(最大500)
* @param pageSize
*/
public void setPageSize(int pageSize) {
if (pageSize == -1 || pageSize > 0 ) {
this.pageSize = pageSize;
} else {
this.pageSize = Integer.valueOf(Configurations.getIntProperty("page.pageSize", 30));
}
}
/**
* 首页索引
* @return
*/
@JsonIgnore
public int getFirst() {
return first;
}
/**
* 尾页索引
* @return
*/
public int getLast() {
return last;
}
/**
* 获取页面总数
* @return getLast();
*/
@JsonIgnore
public int getTotalPage() {
return getLast();
}
/**
* 是否为第一页
* @return
*/
@JsonIgnore
public boolean isFirstPage() {
return firstPage;
}
/**
* 是否为最后一页
* @return
*/
@JsonIgnore
public boolean isLastPage() {
return lastPage;
}
public void setLastPage(boolean lastPage) {
this.lastPage = lastPage;
}
/**
* @return where
*/
@JsonIgnore
public String getWhere() {
return where;
}
/**
* @param where 要设置的 where
*/
public void setWhere(String where) {
this.where = where;
}
/**
* 上一页索引值
* @return
*/
@JsonIgnore
public int getPrev() {
if (isFirstPage()) {
return pageNo;
} else {
return pageNo - 1;
}
}
/**
* 下一页索引值
* @return
*/
@JsonIgnore
public int getNext() {
if (isLastPage()) {
return pageNo;
} else {
return pageNo + 1;
}
}
/**
* 获取本页数据对象列表
* @return List
*/
public List getList() {
return list;
}
/**
* 设置本页数据对象列表
* @param list
*/
public Page setList(List list) {
this.list = list;
initialize();
return this;
}
/**
* 获取查询排序字符串
* @return
*/
@JsonIgnore
public String getOrderBy() {
// SQL过滤,防止注入
String reg = "(?:')|(?:--)|(/\\*(?:.|[\\n\\r])*?\\*/)|"
+ "(\\b(select|update|and|or|delete|insert|trancate|char|into|substr|ascii|declare|exec|count|master|into|drop|execute)\\b)";
Pattern sqlPattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
if (sqlPattern.matcher(orderBy).find()) {
return "";
}
return orderBy;
}
/**
* 设置查询排序,标准查询有效, 实例: updatedate desc, name asc
*/
public void setOrderBy(String orderBy) {
this.orderBy = orderBy;
}
/**
* @return fields 字段属性查询,拼接用,分隔
*/
@JsonIgnore
public String getFields() {
return fields;
}
/**
* @param fields 要设置的 fields
*/
public void setFields(String fields) {
this.fields = fields;
}
/**
* 获取点击页码调用的js函数名称
* function ${page.funcName}(pageNo){location="${ctx}/list-${category.id}${urlSuffix}?pageNo="+i;}
* @return
*/
@JsonIgnore
public String getFuncName() {
return funcName;
}
/**
* 设置点击页码调用的js函数名称,默认为page,在一页有多个分页对象时使用。
* @param funcName 默认为page
*/
public void setFuncName(String funcName) {
this.funcName = funcName;
}
/**
* 获取分页函数的附加参数
* @return
*/
@JsonIgnore
public String getFuncParam() {
return funcParam;
}
@JsonIgnore
public int getMaxExportSize() {
return maxExportSize;
}
public void setMaxExportSize(int maxExportSize) {
this.maxExportSize = maxExportSize;
}
/**
* 设置分页函数的附加参数
* @return
*/
public void setFuncParam(String funcParam) {
this.funcParam = funcParam;
}
/**
* 设置提示消息,显示在“共n条”之后
* @param message
*/
public void setMessage(String message) {
this.message = message;
}
/**
* 分页是否有效
* @return this.pageSize==-1
*/
@JsonIgnore
public boolean isDisabled() {
return this.pageSize==-1;
}
/**
* 是否进行总数统计
* @return this.count==-1
*/
@JsonIgnore
public boolean isNotCount() {
return this.count==-1;
}
/**
* 获取 Hibernate FirstResult
*/
@JsonIgnore
public int getFirstResult(){
int firstResult = (getPageNo() - 1) * getPageSize();
if (firstResult >= getCount()) {
firstResult = 0;
}
return firstResult;
}
/**
* 获取 Hibernate MaxResults
*/
@JsonIgnore
public int getMaxResults(){
return getPageSize();
}
/**
* alias
* @return alias
*/
public String getAlias() {
return alias;
}
/**
* @param alias the alias to set
*/
public void setAlias(String alias) {
this.alias = alias;
}
// /**
// * 获取 Spring data JPA 分页对象
// */
// public Pageable getSpringPage(){
// List orders = new ArrayList();
// if (orderBy!=null){
// for (String order : StringUtils.split(orderBy, ",")){
// String[] o = StringUtils.split(order, " ");
// if (o.length==1){
// orders.add(new Order(Direction.ASC, o[0]));
// }else if (o.length==2){
// if ("DESC".equals(o[1].toUpperCase())){
// orders.add(new Order(Direction.DESC, o[0]));
// }else{
// orders.add(new Order(Direction.ASC, o[0]));
// }
// }
// }
// }
// return new PageRequest(this.pageNo - 1, this.pageSize, new Sort(orders));
// }
//
// /**
// * 设置 Spring data JPA 分页对象,转换为本系统分页对象
// */
// public void setSpringPage(org.springframework.data.domain.Page page){
// this.pageNo = page.getNumber();
// this.pageSize = page.getSize();
// this.count = page.getTotalElements();
// this.list = page.getContent();
// }
}