HttpServletRequest request 获取form参数的两种方式

admin 发布于 2020-03-18T13:30:06 评论(0) 阅读(50)

分类:Java

标签:

@RequestMapping(value="/pay",method = RequestMethod.POST)
	public String buildRequest(HttpServletRequest request){
		Map<String,String> parmMap=new HashMap<String,String>();
		//方式一:getParameterMap(),获得请求参数map
		Map<String,String[]> map= request.getParameterMap();
		//参数名称
		Set<String> key=map.keySet();
		//参数迭代器
		Iterator<String> iterator = key.iterator();
		while(iterator.hasNext()){
			String k=iterator.next();
			parmMap.put(k, map.get(k)[0]);
		}
		System.out.println("parmMap====="+parmMap.toString());
		
		
		//方式二:getParameterNames():获取所有参数名称
		Enumeration<String> a = request.getParameterNames();
		String parm=null;
		String val="";
		while(a.hasMoreElements()){
			//参数名
			parm=a.nextElement();
			//值
			val=request.getParameter(parm);
			parmMap.put(parm, val);
		}
		System.out.println("parmMap=========="+parmMap);
		
		return "";
	}