页面参数自动映射到JavaBean[仿struts 模型驱动]之简易版

        开发使用的基础框架中没有涉及到模型驱动这块,也就是说,没有写方法将前台页面请求参数封装映射到对应的JavaBean属性当中,所以使用这个基础框架开发系统起来,页面的参数一个一个request.getParameter(paramName)取出参数值,然后又一大堆bean.setXXX()才能组装成这个JavaBean对象,重复劳动工作十分明显,且造成后台代码繁杂,不够简单明了。所以为此写了一个简易版的servlet模型驱动,将页面参数自动映射到对应的JavaBean之中,免去大量重复性劳动,使代码更加统一而简洁舒适。
        这里利用了Apache的一个开源框架包(最近老利用Apache的开源包,Apache强大,无私共享精神值得赞叹)commons-beanutils,专门针对JavaBean处理各种操作的,十分方便。

一、先贴官网:
http://commons.apache.org/beanutils/
下载jar包:
http://apache.etoak.com/commons/beanutils/binaries/commons-beanutils-1.8.3-bin.zip
目前版本:commons-beanutils-1.8.3.jar

二、源代码很简单(部分参考到了struts里的思路):

RequestUtils类:
package com.basekj.util;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
 * request操作类
 * @author
 * @version 1.0
 * 2010-8-23 09:46:15
 *
 */
public class RequestUtils {
	
	private static final Log logger = LogFactory.getLog(RequestUtils.class);
	
	/**
	 * 页面参数自动获取并映射到对应的JavaBean上
	 * @param request
	 * @param obj(可配置多个JavaBean,无顺序)
	 */
	public static void getReqParemetersToJavaBean(HttpServletRequest request,Object[] obj) {
		
		HashMap properties = new HashMap();
    	try {
        	Enumeration names = request.getParameterNames();
        	 while (names.hasMoreElements()) {       
                 String name = (String) names.nextElement();
                 Object parameterValue = null;
                 parameterValue = request.getParameterValues(name);
                 properties.put(name, parameterValue);
        	 } 
        	 
        	 if ((obj == null) || (properties == null)) {
                 return;
             }
           
             Iterator entries = properties.entrySet().iterator();
             while (entries.hasNext()) {
                 Map.Entry entry = (Map.Entry)entries.next();
                 String[] nature = ((String) entry.getKey()).split("\\.");
                 String className = "";
                 String propertyName = "";
                 if(nature != null && nature.length == 2) {
                	 className = nature[0];
                	 propertyName = nature[1];
                 } else {
                     continue;
                 }
               for(Object o:obj) {
//                 Class newClass = Class.forName("com.hxkj.entity."+className);
//                 if(newClass.isInstance(o)) {
//                	 BeanUtils.setProperty(o, propertyName, entry.getValue());
//                 }
            	   String beanName = o.getClass().getSimpleName();
                   if(beanName.equals(className)) {
                  	 BeanUtils.setProperty(o, propertyName, entry.getValue());
                   }
               }
             }
    	} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return;
    	}	
	}
}


三、JavaBean代码:
package com.basekj.entity;

/**
 * 
 * @author
 * @date 2010-08-18
 */
public class ParemetersBean {
	private String username;
	private String pwd;
	private int type;
	private String area;
	private String[] fruit;
	private String city;
	
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String[] getFruit() {
		return fruit;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public String getArea() {
		return area;
	}
	public void setArea(String area) {
		this.area = area;
	}
	public void setFruit(String[] fruit) {
		this.fruit = fruit;
	}

}


package com.basekj.entity;
/**
 *
 * @author
 * @date 2010-08-18
 */
public class ParemetersBean2 {
	private String id;
	private String name;
	private String city;
	private String nested;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getNested() {
		return nested;
	}
	public void setNested(String nested) {
		this.nested = nested;
	}
}


四、jsp页面上参数域直接写“JavaBean类名.属性名”即可,以下类型经测试都正确,写法如下:



								<div align="center">
									姓名:
									<input name="ParemetersBean.username" type="text" value="">
								</div>
								<div align="center">
									密码:
									<input name="ParemetersBean.pwd" type="password" value="">
								</div>
								<div align="center">
									类型:
									<select name="ParemetersBean.type">
										<option value="1">
											a
										</option>
										<option value="2">
											b
										</option>
										<option value="3">
											c
										</option>
										<option value="4">
											d
										</option>
									</select>
								</div>
								<div>
									<input type="checkbox" name="ParemetersBean.fruit"
										value="apple">
									苹果
									<br>
									<input type="checkbox" name="ParemetersBean.fruit"
										value="orange">
									桔子
									<br>
									<input type="checkbox" name="ParemetersBean.fruit"
										value="mango">
									芒果
									<br>
								</div>
								<div>
									<input type="radio" name="ParemetersBean.city" value="hf">
									合肥
									<br>
									<input type="radio" name="ParemetersBean.city" value="nj">
									南京
									<br>
									<input type="radio" name="ParemetersBean.city" value="sh">
									上海
									<br>
								</div>
								<div align="center">
									文本域:
									<textarea rows="2" name="ParemetersBean.area"></textarea>
								</div>
							<div align="center">
									id:
									<input name="ParemetersBean2.id" type="text" value="">
								</div>
								<div align="center">
									name:
									<input name="ParemetersBean2.name" type="text" value="">
								</div>
								<div>
									这个参数没有对应的JavaBean属性,不会做映射处理,也不会影响其他属性
									<input name="ParemetersBean2.efdfvfdsf" type="text" value="">
								</div>
								<div>
									<input type="radio" name="ParemetersBean2.city" value="dj">
									东京
									<br>
									<input type="radio" name="ParemetersBean2.city" value="ny">
									纽约
									<br>
									<input type="radio" name="ParemetersBean2.city" value="bl">
									巴黎
									<br>
								</div>

								



五、源代码摘选(BeanUtilsBean.java):

/**
     * <p>Set the specified property value, performing type conversions as
     * required to conform to the type of the destination property.</p>
     *
     * <p>If the property is read only then the method returns 
     * without throwing an exception.</p>
     *
     * <p>If <code>null</code> is passed into a property expecting a primitive value,
     * then this will be converted as if it were a <code>null</code> string.</p>
     *
     * <p><strong>WARNING</strong> - The logic of this method is customized
     * to meet the needs of <code>populate()</code>, and is probably not what
     * you want for general property copying with type conversion.  For that
     * purpose, check out the <code>copyProperty()</code> method instead.</p>
     *
     * <p><strong>WARNING</strong> - PLEASE do not modify the behavior of this
     * method without consulting with the Struts developer community.  There
     * are some subtleties to its functionality that are not documented in the
     * Javadoc description above, yet are vital to the way that Struts utilizes
     * this method.</p>
     *
     * @param bean Bean on which setting is to be performed
     * @param name Property name (can be nested/indexed/mapped/combo)
     * @param value Value to be set
     *
     * @exception IllegalAccessException if the caller does not have
     *  access to the property accessor method
     * @exception InvocationTargetException if the property accessor method
     *  throws an exception
     */
    public void setProperty(Object bean, String name, Object value)
        throws IllegalAccessException, InvocationTargetException {

        // Trace logging (if enabled)
        if (log.isTraceEnabled()) {
            StringBuffer sb = new StringBuffer("  setProperty(");
            sb.append(bean);
            sb.append(", ");
            sb.append(name);
            sb.append(", ");
            if (value == null) {
                sb.append("<NULL>");
            } else if (value instanceof String) {
                sb.append((String) value);
            } else if (value instanceof String[]) {
                String[] values = (String[]) value;
                sb.append('[');
                for (int i = 0; i < values.length; i++) {
                    if (i > 0) {
                        sb.append(',');
                    }
                    sb.append(values[i]);
                }
                sb.append(']');
            } else {
                sb.append(value.toString());
            }
            sb.append(')');
            log.trace(sb.toString());
        }

        // Resolve any nested expression to get the actual target bean
        Object target = bean;
        Resolver resolver = getPropertyUtils().getResolver();
        while (resolver.hasNested(name)) {
            try {
                target = getPropertyUtils().getProperty(target, resolver.next(name));
                name = resolver.remove(name);
            } catch (NoSuchMethodException e) {
                return; // Skip this property setter
            }
        }
        if (log.isTraceEnabled()) {
            log.trace("    Target bean = " + target);
            log.trace("    Target name = " + name);
        }

        // Declare local variables we will require
        String propName = resolver.getProperty(name); // Simple name of target property
        Class type = null;                            // Java type of target property
        int index  = resolver.getIndex(name);         // Indexed subscript value (if any)
        String key = resolver.getKey(name);           // Mapped key value (if any)

        // Calculate the property type
        if (target instanceof DynaBean) {
            DynaClass dynaClass = ((DynaBean) target).getDynaClass();
            DynaProperty dynaProperty = dynaClass.getDynaProperty(propName);
            if (dynaProperty == null) {
                return; // Skip this property setter
            }
            type = dynaProperty.getType();
        } else if (target instanceof Map) {
            type = Object.class;
        } else if (target != null && target.getClass().isArray() && index >= 0) {
            type = Array.get(target, index).getClass();
        } else {
            PropertyDescriptor descriptor = null;
            try {
                descriptor =
                    getPropertyUtils().getPropertyDescriptor(target, name);
                if (descriptor == null) {
                    return; // Skip this property setter
                }
            } catch (NoSuchMethodException e) {
                return; // Skip this property setter
            }
            if (descriptor instanceof MappedPropertyDescriptor) {
                if (((MappedPropertyDescriptor) descriptor).getMappedWriteMethod() == null) {
                    if (log.isDebugEnabled()) {
                        log.debug("Skipping read-only property");
                    }
                    return; // Read-only, skip this property setter
                }
                type = ((MappedPropertyDescriptor) descriptor).
                    getMappedPropertyType();
            } else if (index >= 0 && descriptor instanceof IndexedPropertyDescriptor) {
                if (((IndexedPropertyDescriptor) descriptor).getIndexedWriteMethod() == null) {
                    if (log.isDebugEnabled()) {
                        log.debug("Skipping read-only property");
                    }
                    return; // Read-only, skip this property setter
                }
                type = ((IndexedPropertyDescriptor) descriptor).
                    getIndexedPropertyType();
            } else if (key != null) {
                if (descriptor.getReadMethod() == null) {
                    if (log.isDebugEnabled()) {
                        log.debug("Skipping read-only property");
                    }
                    return; // Read-only, skip this property setter
                }
                type = (value == null) ? Object.class : value.getClass();
            } else {
                if (descriptor.getWriteMethod() == null) {
                    if (log.isDebugEnabled()) {
                        log.debug("Skipping read-only property");
                    }
                    return; // Read-only, skip this property setter
                }
                type = descriptor.getPropertyType();
            }
        }

        // Convert the specified value to the required type
        Object newValue = null;
        if (type.isArray() && (index < 0)) { // Scalar value into array
            if (value == null) {
                String[] values = new String[1];
                values[0] = null;
                newValue = getConvertUtils().convert(values, type);
            } else if (value instanceof String) {
                newValue = getConvertUtils().convert(value, type);
            } else if (value instanceof String[]) {
                newValue = getConvertUtils().convert((String[]) value, type);
            } else {
                newValue = convert(value, type);
            }
        } else if (type.isArray()) {         // Indexed value into array
            if (value instanceof String || value == null) {
                newValue = getConvertUtils().convert((String) value,
                                                type.getComponentType());
            } else if (value instanceof String[]) {
                newValue = getConvertUtils().convert(((String[]) value)[0],
                                                type.getComponentType());
            } else {
                newValue = convert(value, type.getComponentType());
            }
        } else {                             // Value into scalar
            if (value instanceof String) {
                newValue = getConvertUtils().convert((String) value, type);
            } else if (value instanceof String[]) {
                newValue = getConvertUtils().convert(((String[]) value)[0],
                                                type);
            } else {
                newValue = convert(value, type);
            }
        }

        // Invoke the setter method
        try {
          getPropertyUtils().setProperty(target, name, newValue);
        } catch (NoSuchMethodException e) {
            throw new InvocationTargetException
                (e, "Cannot set " + propName);
        }

    }


六、总结
        目前只是简易版参数映射,能做到日常开发一般性处理要求,看了Apache beanutils包相关源代码,处理得挺细致的,功能应该很强大,我只是用到了一点点而已,还没处理二进制的方式提交表单enctype=“multipart/form-data”的情况,等着以后慢慢细致化吧。

你可能感兴趣的:(apache,框架,bean,servlet,struts)