BeanUtils
BeanUtils是第三方工具类,它可以一次性地把所有请求的参数注入到JavaBean中
引入依赖


使用BeanUtils实现注入
User user = new User(); BeanUtils.populate(user, req.getParameterMap());
封装成一个工具类
package com.tang.web.servlet.utils;
import org.apache.commons.beanutils.BeanUtils;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
public class MyBeanUtils {
/**
* 把Map中的值注入到JavaBean对应的属性中
* @param bean
* @param value
* @param <T>
* @return
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
public static <T> T copyParamsToBean(T bean, Map value) throws InvocationTargetException, IllegalAccessException {
BeanUtils.populate(bean, value);
return bean;
}
}
浙公网安备 33010602011771号