1. 程式人生 > >通用型 控制層接受引數方法

通用型 控制層接受引數方法

/**
* 獲取提交引數並轉為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;
}