1. 程式人生 > >自實現後臺動態請求引數:getParam

自實現後臺動態請求引數:getParam

/**
 * 得到request物件
 */
public HttpServletRequest getRequest() {
	HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	return request;
}
/*
 * 得到引數Map
 */
public Map<String,Object> getParam(HttpServletRequest req)
{
	Map param=req.getParameterMap();
	Map<String,Object> map=new HashMap<String, Object>();
	Set<Entry<String, Object>> sets=param.entrySet();
	for (Entry<String, Object> entry : sets) {
		String key=entry.getKey();
		Object valObj=entry.getValue();
		String value="";
		if(null==valObj)
		{
			value="";
		}
		else if(valObj instanceof String[])
		{
			String[] values=(String[]) valObj;
			for (String string : values) {
				value+=string+",";
			}
			value= value.substring(0, value.length()-1);
		}
		else
		{
			value=valObj.toString();
		}
		map.put(key, value);
	}
	return map; 
}
/*
 * 呼叫
 */
public static Map<String,Object> getMap(){
	return getParam(getRequest());
}


@RequestMapping("/……")
@ResponseBody
public …… getInfoDetail(){
	
	Map<String,Object> param=getMap();
	
	……
}