java反射封装表单请求数据至对象

页面端:

 

 	<form id="domainForm"  style="margin:0px;padding:0px;">
  	<table class="editTable">
    	<tr>
    		<td class="tTitle">用户名:</td>
    		<td><input type="text" id="userName" name="userName" value="用户"></td>
    		<td class="tTitle">技能:</td>
    		<td>
    			<select id="skill" name="skill" multiple="multiple" size="1">
    				<option value="java">java</option>
    				<option value="C#">C#</option>
    				<option value=".net">.net</option>
    				<option value="拍马屁">拍马屁</option>
    			</select>
    		</td>
    	</tr>
    	<tr>
    		<td class="tTitle">电话1:</td>
    		<td><input type="text" id="phone" name="phone" value="p1"></td>
    		<td class="tTitle">电话2:</td>
    		<td><input type="text" id="phone" name="phone" value="p2"></td>
    	</tr>
    	<tr>
    		<td class="tTitle">爱好:</td>
    		<td >
    			<input type="checkbox" name="hobby" value="swim">游泳
    			<input type="checkbox" name="hobby" value="fitness">健身
    			<input type="checkbox" name="hobby" value="net">上网
    			<input type="checkbox" name="hobby" value="ball">篮球
    		</td>
    		<td class="tTitle">住址:</td>
    		<td><input type="text" id="address" name="address" value="addr&sex=man"></td>
    	</tr>
    </table>

提交数据  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  这样规避了提交的值与传递的格式之间的冲突。

 

服务端获取请求的值,并且组装到对象中

 

 

User user=new User();
Method[] methods=User.class.getMethods();
for(int i=0;i<methods.length;i++){
	Method method=methods[i];
	for(Iterator<Map.Entry<String, String[]>> iterator=map.entrySet().iterator();iterator.hasNext();){
		Entry<String, String[]> entry=(Entry)iterator.next();
		String key=entry.getKey();
		String[] values=entry.getValue();
		String vals="";
		for(String val : values){
			vals+=","+val;
		}
		String methodStr="set"+key.substring(0,1).toUpperCase()+key.substring(1);
		if(methodStr.equals(method.getName())){
			method.invoke(user, vals.substring(1));
		}
	}
}
 

 

 

修改如上实现:

 

	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);
		}
	}
 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值