java反射封装表单请求数据至对象
页面端:
?
提交数据 ?var dates=$("#domainForm").serialize(); 我们会看到,这个值为:
userName=%E7%94%A8%E6%88%B7&skill=java&skill=.net&phone=p1&phone=p2
&hobby=swim&hobby=net&address=addr%26sex%3Dman
?
由此可看出,不管是checkbox,select,或者故意放置多个name相同的input框,传送数据时都是 &变量名A(可能相同多个)=值B 的形式组装,值为addr&sex=man 中的&被转为%26 ,?=转为%3D ?这样规避了提交的值与传递的格式之间的冲突。
?
服务端获取请求的值,并且组装到对象中
?
?
public <T> T fireDomain(HttpServletRequest request,Class<T> clazz) throws Exception{Map map=request.getParameterMap();if(map==null || map.isEmpty())return null;//生成对象,默认有无参构造方法T t=clazz.newInstance();Method[] methods=clazz.getMethods();//保存返回各种类型参数值列表List list=new ArrayList();//定位具体位置参数int j=0;for(int i=0;i<methods.length;i++){Method method=methods[i];//遍历for(Iterator<Map.Entry<String, Object[]>> iterator=map.entrySet().iterator();iterator.hasNext();){Entry<String, String[]> entry=(Entry)iterator.next();//获取request请求的参数名String key=entry.getKey();//对应的参数值,是个字符串数组,String[] values=entry.getValue();String methodStr=key.substring(0,1).toUpperCase()+key.substring(1);//判断是否为domain属性的set方法且其参数与request传递过来的参数一样if(method.getName().startsWith("set") && methodStr.equals(method.getName().substring(3))){this.fomartType(list,method,values);method.invoke(t, list.get(j++));}}}return t;}//没有考虑多维数组public void fomartType(List list,Method method,String[] values) throws Exception{if(values==null || values.length<1){list.add(null);return;}String val0=values[0];Type type=method.getParameterTypes()[0];if(type==String.class){list.add(StringUtils.join(values));}else if(type == int.class || type== Integer.class){list.add(Integer.valueOf(val0));}else if(type == float.class || type == Float.class){list.add(Float.valueOf(val0));}else if(type == long.class || type == Long.class){list.add(Long.valueOf(val0));}else if(type == double.class || type == Double.class){list.add(Double.valueOf(val0));}else if(type == short.class || type == Short.class){list.add(Short.valueOf(val0));}else if(type == char.class || type == Character.class){list.add(val0.charAt(0));}else if(type == boolean.class || type == Boolean.class){list.add(val0.equalsIgnoreCase("true") || Integer.valueOf(val0)>0);}else if(type == byte.class || type == Byte.class){list.add(Byte.valueOf(val0));}else if(type == String[].class){list.add(values);}else if(type == int[].class || type == Integer[].class){list.add(this.stringArr2IntegerArr(values));}else if(type == float[].class || type == Float[].class){list.add(this.stringArr2FloatArr(values));}else if(type == long[].class || type == Long[].class){list.add(this.stringArr2LongArr(values));}else if(type == double[].class || type == Double[].class){list.add(this.stringArr2DoubleArr(values));}else if(type == short[].class || type == Short[].class){list.add(this.stringArr2ShortArr(values));}else if(type == char[].class || type == Character[].class){list.add(this.stringArr2CharacterArr(values));}else if(type == boolean[].class || type == Boolean[].class){list.add(this.stringArr2BooleanArr(values));}else if(type == byte[].class || type == Byte[].class){list.add(this.stringArr2ByteArr(values));}else{list.add(null);}}??
?
?
?
?