javaweb实现自动将前端的属性值(HttpServletRequest)转化为后台对应的实体类

时间:2025-02-24 11:32:28

通常我们在后台需要获取前端的属性值的时候用到的是("xxx");

下面的方法将实现自动将HttpServletRequest中的值封装到实体类中,需要一定的Java反射技术。

要求:前端form表单中的input标签中name的值是实体类中的属性。这样才能被识别到。

例如:

在JSP中使用如下input:

<input type="text" name="age" value=""/>
在实体类中有age属性并实现get和set方法,然后在controller(springMVC)层或者action(Structs2)中使用Person person = (request, );方法就可以自动将input标签age 的值设置到Person实例化对象person中的age属性里。

下面方法中的(value);使用到时间工具类,请参考我的另一篇博客/bq1073100909/article/details/49472615

package ;

import ;
import ;
import ;
import ;

/**
 * 通过反射技术将HttpServletRequest中的前端属性数据赋值到对应的实体类中,使用或修改本类需要熟悉Java反射技术
 * 使用方法:Xxxx为相应的实体类
 * (HttpServletRequest, );
 */
public class DTOBuilder {
    
    /**
     * 方法入口,得到Dto
     *@param request 
     *@param dtoClass 传入的实体类
     *@return
     */
    public static Object getDTO(HttpServletRequest request, Class dtoClass) {
        Object dtoObj = null;
        if ((dtoClass == null) || (request == null))
            return dtoObj;
        try {
            //实例化对象
            dtoObj = ();
            setDTOValue(request, dtoObj);
        } catch (Exception ex) {
            ();
        }
        return dtoObj;
    }
    /**
     * 保存数据 
     *@param request
     *@param dto
     *@throws Exception
     */
    public static void setDTOValue(HttpServletRequest request, Object dto) throws Exception {
        if ((dto == null) || (request == null))
            return;
        //得到类中所有的方法 基本上都是set和get方法
        Method[] methods = ().getMethods();
        for (int i = 0; i < ; i++) {
            try {
                //方法名
                String methodName = methods[i].getName();
                //方法参数的类型
                Class[] type = methods[i].getParameterTypes();
                //当时set方法时,判断依据:setXxxx类型
                if ((() > 3) && (("set")) && ( == 1)) {
                    //将set后面的大写字母转成小写并截取出来
                    String name = (3, 4).toLowerCase() + (4);
                    Object objValue = getBindValue(request, name, type[0]);
                    if (objValue != null) {
                        Object[] value = { objValue };
                        invokeMothod(dto, methodName, type, value);
                    }
                }
            } catch (Exception ex) {
                throw ex;
            }
        }
    }
    /**
     * 通过request得到相应的值
     *@param request HttpServletRequest
     *@param bindName 属性名
     *@param bindType 属性的类型
     *@return
     */
    public static Object getBindValue(HttpServletRequest request, String bindName, Class bindType) {
        //得到request中的值
        String value = (bindName);
        if (value != null) {
            value = ();
        }
        return getBindValue(value, bindType);
    }
    /**
     * 通过调用方法名(setXxxx)将值设置到属性中
     *@param classObject 实体类对象
     *@param strMethodName 方法名(一般都是setXxxx)
     *@param argsType 属性类型数组
     *@param args 属性值数组
     *@return
     *@throws NoSuchMethodException
     *@throws SecurityException
     *@throws IllegalAccessException
     *@throws IllegalArgumentException
     *@throws InvocationTargetException
     */
    public static Object invokeMothod(Object classObject, String strMethodName, Class[] argsType, Object[] args)
            throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException,
            InvocationTargetException {
        //得到classObject这个类的方法
        Method concatMethod = ().getMethod(strMethodName, argsType);
        //调用方法将classObject赋值到相应的属性
        return (classObject, args);
    }
    /**
     * 根据bindType类型的不同转成相应的类型值
     *@param value String类型的值,要根据bindType类型的不同转成相应的类型值
     *@param bindType 属性的类型
     *@return
     */
    public static Object getBindValue(String value, Class bindType) {
        if ((value == null) || (().length() == 0))
            return null;
        String typeName = ();
        //依次判断各种类型并转换相应的值
        if ((""))
            return value;
        if (("int"))
            return new Integer(value);
        if (("long"))
            return new Long(value);
        if (("boolean"))
            return new Boolean(value);
        if (("float"))
            return new Float(value);
        if (("double"))
            return new Double(value);
        if (("")) {
            if ("NaN.00".equals(value))
                return new BigDecimal("0");
            return new BigDecimal(());
        }
        if ((""))
            //参考方法,value如果是时间类型,必须是yyyy-MM-dd格式才能被识别
            //请参考我的另一篇博客/bq1073100909/article/details/49472615
            return (value);
        if ((""))
            return new Integer(value);
        if (("")) {
            return new Long(value);
        }
        if (("")) {
            return new Boolean(value);
        }
        return value;
    }
}