通用型 控制层接受参数方法

/**
* 获取提交参数并转为map(分页)
* @return 
*/
public Map<String, Object> getPageInfo(){
Map<String, Object> map =new HashMap<String, Object>();
Map<String, String[]> parasMap = getHttpServletRequest().getParameterMap();
for (Entry<String, String[]> e : parasMap.entrySet()) {
String key = e.getKey();
String[] values = e.getValue();
try {
// Object value = Converter.convert(colType, paraValue != null ? paraValue[0] : null);
Object value = values[0]!= null?values[0]:null;
map.put(key, value);
} catch (Exception ex) {
ex.printStackTrace();
}
}

//获取页码信息
// map.put(PAGESIZE,getPageSize());
// map.put(PAGENUM,getPageNumber());

int firstRow=getParaToInt(OFFSET,0);
int lastRow=firstRow+getPageSize();
map.put("firstRow",firstRow);
map.put("lastRow",lastRow);

String sort=getPara(SORT,"");

//获取关键字信息
map.put(SEARCH,getPara(SEARCH));

//获取排序信息
if(sort!=null&&!"".equals(sort)){
map.put("orderBy", "order by "+sort+" "+getPara(ORDER,""));
}

return map;

}

public Integer getParaToInt(String name, Integer defaultValue) {
return toInt(getHttpServletRequest().getParameter(name), defaultValue);
}

/**
* Returns the value of a request parameter as a String, or default value if the parameter does not exist.
* @param name a String specifying the name of the parameter
* @param defaultValue a String value be returned when the value of parameter is null
* @return a String representing the single value of the parameter
*/
public String getPara(String name) {
String result = getHttpServletRequest().getParameter(name);
return result != null && !"".equals(result) ? result : "";
}


//--------------------------------bootstrap table--------------------------------
















public static int PAGE_SIZE=10; //默认每页条数
public static final String LIMIT="limit"; //分页条数
public static final String OFFSET="offset"; //从第offset条开始查询
public static final String SEARCH="search"; //查询关键字
public static final String SORT="sort"; //排序字段 
public static final String ORDER="order"; //排序类型  asc desc
public static final String PAGENUM="pagenum"; //页码
public static final String PAGESIZE="pagesize"; //页数

/**
* 获取提交参数并转为map(分页)
* @return 
*/
public Map<String, Object> getPageInfo(){
Map<String, Object> map =new HashMap<String, Object>();
Map<String, String[]> parasMap = getHttpServletRequest().getParameterMap();
for (Entry<String, String[]> e : parasMap.entrySet()) {
String key = e.getKey();
String[] values = e.getValue();
try {
// Object value = Converter.convert(colType, paraValue != null ? paraValue[0] : null);
Object value = values[0]!= null?values[0]:null;
map.put(key, value);
} catch (Exception ex) {
ex.printStackTrace();
}
}

//获取页码信息
// map.put(PAGESIZE,getPageSize());
// map.put(PAGENUM,getPageNumber());

int firstRow=getParaToInt(OFFSET,0);
int lastRow=firstRow+getPageSize();
map.put("firstRow",firstRow);
map.put("lastRow",lastRow);

String sort=getPara(SORT,"");

//获取关键字信息
map.put(SEARCH,getPara(SEARCH));

//获取排序信息
if(sort!=null&&!"".equals(sort)){
map.put("orderBy", "order by "+sort+" "+getPara(ORDER,""));
}

return map;
}

/**
* 获取每页条数
* @param name
* @return
*/
public Integer getPageSize() {
int pageSize=getParaToInt(LIMIT,PAGE_SIZE);
return pageSize;
}

/**
* 获取当前页码
* @param name
* @return
*/
public Integer getPageNumber() {
int pageSize=getPageSize();
int offset=getParaToInt(OFFSET,0);
int pageNumber=offset/pageSize;

return pageNumber;
}

/**
* 获取排序信息
* @param name
* @return
*/
public OrderPart getOrderPart() {
return OrderPart.newInstance().and(getPara(SORT),getPara(ORDER));
}


private static Integer toInt(String value, Integer defaultValue) {
if (value == null || "".equals(value.trim()))
return defaultValue;
if (value.startsWith("N") || value.startsWith("n"))
return -Integer.parseInt(value.substring(1));
return Integer.parseInt(value);
}

/**
* Returns the value of a request parameter and convert to Integer with a default value if it is null.
* @param name a String specifying the name of the parameter
* @return a Integer representing the single value of the parameter
*/
public Integer getParaToInt(String name, Integer defaultValue) {
return toInt(getHttpServletRequest().getParameter(name), defaultValue);
}

/**
* Returns the value of a request parameter as a String, or default value if the parameter does not exist.
* @param name a String specifying the name of the parameter
* @param defaultValue a String value be returned when the value of parameter is null
* @return a String representing the single value of the parameter
*/
public String getPara(String name) {
String result = getHttpServletRequest().getParameter(name);
return result != null && !"".equals(result) ? result : "";
}

/**
* Returns the value of a request parameter as a String, or default value if the parameter does not exist.
* @param name a String specifying the name of the parameter
* @param defaultValue a String value be returned when the value of parameter is null
* @return a String representing the single value of the parameter
*/
public String getPara(String name, String defaultValue) {
String result = getHttpServletRequest().getParameter(name);
return result != null && !"".equals(result) ? result : defaultValue;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值