コード例 #1
0
 static {
   try {
     requestClass = Class.forName("javax.servlet.ServletRequest");
     getParameterMap = requestClass.getMethod("getParameterMap", new Class[] {});
     hasRequest = true;
   } catch (Exception e) {
     hasRequest = false;
   }
 }
コード例 #2
0
 /**
  * 对象中获取分页参数
  *
  * @param params
  * @return
  */
 public static Page getPageFromObject(Object params) {
   int pageNum;
   int pageSize;
   MetaObject paramsObject = null;
   if (params == null) {
     throw new NullPointerException("分页查询参数params不能为空!");
   }
   if (hasRequest && requestClass.isAssignableFrom(params.getClass())) {
     try {
       paramsObject = SystemMetaObject.forObject(getParameterMap.invoke(params, new Object[] {}));
     } catch (Exception e) {
       // 忽略
     }
   } else {
     paramsObject = SystemMetaObject.forObject(params);
   }
   if (paramsObject == null) {
     throw new NullPointerException("分页查询参数params处理失败!");
   }
   try {
     pageNum = Integer.parseInt(String.valueOf(getParamValue(paramsObject, "pageNum", true)));
     pageSize = Integer.parseInt(String.valueOf(getParamValue(paramsObject, "pageSize", true)));
   } catch (NumberFormatException e) {
     throw new IllegalArgumentException("分页参数不是合法的数字类型!");
   }
   Object _count = getParamValue(paramsObject, "count", false);
   boolean count = true;
   if (_count != null) {
     count = Boolean.valueOf(String.valueOf(_count));
   }
   Page page = new Page(pageNum, pageSize, count);
   Object reasonable = getParamValue(paramsObject, "reasonable", false);
   if (reasonable != null) {
     page.setReasonable(Boolean.valueOf(String.valueOf(reasonable)));
   }
   Object pageSizeZero = getParamValue(paramsObject, "pageSizeZero", false);
   if (pageSizeZero != null) {
     page.setPageSizeZero(Boolean.valueOf(String.valueOf(pageSizeZero)));
   }
   return page;
 }