Spring mvc中把表单数据当做一个对象提交给controller的实现

简版Spring源码(帮助理解):

protected  T getParamByBean(HttpServletRequest request, Class clazz) {  
    T bean = null;  
    try {  
        bean = clazz.newInstance();  
    } catch (InstantiationException e) {  
        log.debug(e.getMessage());  
    } catch (IllegalAccessException e) {  
        log.debug(e.getMessage());  
    }  

    Enumeration<String> enums = request.getParameterNames();  
    while (enums.hasMoreElements()) {  
        String key = enums.nextElement();  
        String val = request.getParameter(key);  
        if (!BaseUtils.isEmpty(val))  
            ReflectUtils.setFieldValueIgnoreCase(bean, key, val);  
    }  

    return bean;  
}  

你可能感兴趣的:(ssm学习)