1个比较简单的使用java反射机制获取前台数据进行数据封装的例子
//利用反射机制 和 从request获取所有参数的形式来构建查询对象 CustomerChooseSearchParma searchObject = new CustomerChooseSearchParma(); //不是数组类型的参数的list List<String> singleList = getSingleStringFieldList(); Map map = request.getParameterMap(); Set keSet = map.entrySet(); for(Iterator itr=keSet.iterator();itr.hasNext();){ Map.Entry me = (Map.Entry)itr.next(); String ok= (String)me.getKey(); Object ov= me.getValue(); if(StringUtils.isNotBlank(ok) && !"action".equals(ok)){ String methodName = "set" + ok.substring(0, 1).toUpperCase() + ok.substring(1); String[] value=new String[1]; if(ov instanceof String[]){ value=(String[])ov; }else{ value[0]=ov.toString(); } Method m = null; if(singleList.contains(ok)){ m = CustomerChooseSearchParma.class.getMethod(methodName, String.class); m.invoke(searchObject, value[0]); }else{ m = CustomerChooseSearchParma.class.getMethod(methodName, String[].class); m.invoke(searchObject, new Object[]{value}); } } }