在HttpServletRequest request获得参数,通过类的set方法封装到对象中


public static void setAttributes(HttpServletRequest req,Object obj) throws Exception{
		Map map=req.getParameterMap();
		for (String key : map.keySet()) {
				Class objClass = (Class) obj.getClass();
				Field[] fs = objClass.getDeclaredFields ();
				for(Field f:fs){
					f.setAccessible( true );
					if(f.getName().equals(key)){
						String type = f.getType().toString();
						if (type.endsWith( "String" )) {
							f.set(obj,map.get(key)[0]);
						}else if (type.endsWith( "int" ) || type.endsWith( "Integer" )){
							f.set(obj,Integer.valueOf(map.get(key)[0]));
						}
					}
				}
		}
	}

你可能感兴趣的:(经典)