SpringSide封装Page类

package org.springside.modules.orm;

import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.StringUtils;


public class Page<T> {
 // 公共变量 //
 public static final String ASC = "asc";
 public static final String DESC = "desc";

 //分页参数 //
 protected int pageNo = 1;
 protected int pageSize = 1;
 protected String orderBy = null;
 protected String order = null;
 protected boolean autoCount = true;

 //返回结果 //
 protected List<T> result = Collections.emptyList();
 protected long totalCount = -1;

 // 构造函数 //

 public Page() {
 }

 public Page(final int pageSize) {
  setPageSize(pageSize);
 }

 public Page(final int pageSize, final boolean autoCount) {
  setPageSize(pageSize);
  setAutoCount(autoCount);
 }

 // 查询参数访问函数 //

 
 public int getPageNo() {
  return pageNo;
 }

 
 public void setPageNo(final int pageNo) {
  this.pageNo = pageNo;

  if (pageNo < 1) {
   this.pageNo = 1;
  }
 }

 
 public int getPageSize() {
  return pageSize;
 }

 
 public void setPageSize(final int pageSize) {
  this.pageSize = pageSize;

  if (pageSize < 1) {
   this.pageSize = 1;
  }
 }

 
 public int getFirst() {
  return ((pageNo - 1) * pageSize) + 1;
 }

 
 public String getOrderBy() {
  return orderBy;
 }

 
 public void setOrderBy(final String orderBy) {
  this.orderBy = orderBy;
 }

 
 public boolean isOrderBySetted() {
  return (StringUtils.isNotBlank(orderBy) && StringUtils.isNotBlank(order));
 }

 
 public String getOrder() {
  return order;
 }

 
 public void setOrder(final String order) {
  //检查order字符串的合法值
  String[] orders = StringUtils.split(StringUtils.lowerCase(order), ',');
  for (String orderStr : orders) {
   if (!StringUtils.equals(DESC, orderStr) && !StringUtils.equals(ASC, orderStr))
    throw new IllegalArgumentException("排序方向" + orderStr + "不是合法值");
  }

  this.order = StringUtils.lowerCase(order);
 }

 
 public boolean isAutoCount() {
  return autoCount;
 }

 
 public void setAutoCount(final boolean autoCount) {
  this.autoCount = autoCount;
 }

 // 访问查询结果函数 //

 
 public List<T> getResult() {
  return result;
 }

 public void setResult(final List<T> result) {
  this.result = result;
 }

 
 public long getTotalCount() {
  return totalCount;
 }

 public void setTotalCount(final long totalCount) {
  this.totalCount = totalCount;
 }

 
 public long getTotalPages() {
  if (totalCount < 0)
   return -1;

  long count = totalCount / pageSize;
  if (totalCount % pageSize > 0) {
   count++;
  }
  return count;
 }

 
 public boolean isHasNext() {
  return (pageNo + 1 <= getTotalPages());
 }

 
 public int getNextPage() {
  if (isHasNext())
   return pageNo + 1;
  else
   return pageNo;
 }

 
 public boolean isHasPre() {
  return (pageNo - 1 >= 1);
 }

 
 public int getPrePage() {
  if (isHasPre())
   return pageNo - 1;
  else
   return pageNo;
 }
}

package com.model; import javax.servlet.http.HttpServletRequest; import java.util.*; /** * 说明:参数封装Map * @version */ public class PageData extends HashMap implements Map{ private static final long serialVersionUID = 1L; Map map = null; HttpServletRequest request; public PageData(HttpServletRequest request){ this.request = request; Map properties = request.getParameterMap(); Map returnMap = new HashMap(); Iterator entries = properties.entrySet().iterator(); Entry entry; String name = ""; String value = ""; while (entries.hasNext()) { entry = (Entry) entries.next(); name = (String) entry.getKey(); Object valueObj = entry.getValue(); if(null == valueObj){ value = ""; }else if(valueObj instanceof String[]){ String[] values = (String[])valueObj; for(int i=0;i<values.length;i++){ value = values[i] + ","; } value = value.substring(0, value.length()-1); }else{ value = valueObj.toString(); } returnMap.put(name, value); } map = returnMap; } public PageData() { map = new HashMap(); } @Override public Object get(Object key) { Object obj = null; if(map.get(key) instanceof Object[]) { Object[] arr = (Object[])map.get(key); obj = request == null ? arr:(request.getParameter((String)key) == null ? arr:arr[0]); } else { obj = map.get(key); } return obj; } public static boolean hasVal(PageData pd,String key){ return pd!=null&&pd;.containsKey(key)&&!pd.getString(key).isEmpty(); } public String getString(Object key) { return (String)get(key); } public Integer getInt(Object key) { return Integer.parseInt(getString(key)); } @SuppressWarnings("unchecked") @Override public Object put(Object key, Object value) { return map.put(key, value); } @Override public Object remove(Object key) { return map.remove(key); } public void clear() { map.clear(); } public boolean containsKey(Object key) { // TODO Auto-generated method stub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值