WebUtils复用代码【request2Bean、UUID】

request封装到Bean对象


    public static <T> T request2Bean(HttpServletRequest httpServletRequest, Class<T> aClass) {


        try {
            //对于日期而言,是需要日期转换器的
            ConvertUtils.register(new DateLocaleConverter(), Date.class);

            //获取Bean的对象
            T bean = aClass.newInstance();

            httpServletRequest.setCharacterEncoding("UTF-8");

            //获取表单中所有的名字
            Enumeration enumeration = httpServletRequest.getParameterNames();

            //遍历表单提交过来的名字
            while (enumeration.hasMoreElements()) {

                //每个名字
                String name = (String) enumeration.nextElement();

                //获取得到值
                String value = httpServletRequest.getParameter(name);

                //如果用户提交的数据不为空,那么将数据封装到Bean中
                if (!value.equals("") && value != null) {
                    BeanUtils.setProperty(bean, name, value);
                }
            }
            return bean;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("封装数据到Bean中,失败了!");
        }
    }

UUID工具方法


    public static String makeId() {
        return UUID.randomUUID().toString();
    }
如果您觉得这篇文章帮助到了您,可以给作者一点鼓励



原文地址:https://www.cnblogs.com/zhong-fucheng/p/7203010.html