zoukankan      html  css  js  c++  java
  • 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();
        }
    
    如果您觉得这篇文章帮助到了您,可以给作者一点鼓励



  • 相关阅读:
    leetcode 451 根据字符出现频率排序
    leetcode 1833 雪糕的最大数量
    leetcode 166 Excel表列名称
    877 石子游戏
    01 背包问题
    正则表达式
    leetcode 160 相交链表
    git 备忘录
    leetcode 525 连续数组
    Mysite
  • 原文地址:https://www.cnblogs.com/zhong-fucheng/p/7203010.html
Copyright © 2011-2022 走看看