zoukankan      html  css  js  c++  java
  • commons-beanutil应用

    利用commons-beanutil自写一个java工具类:

    public class MapToBeanUtil {
    
        /*
         * 传入一个map和一个类型,通过类型创建变量,把map中的数据封装到javabean对象中,然后返回对象
         */
        public static <T> T toBean(Map map,Class<T>clazz ) {
            try {
                T bean = clazz.newInstance();
                BeanUtils.populate(bean, map);
                return bean;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    再准备一个javabean:

    public class User {
        private String name;
        private String password;
        private String sex;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        public String getSex() {
            return sex;
        }
        public void setSex(String sex) {
            this.sex = sex;
        }
        @Override
        public String toString() {
            return "Student [name=" + name + ", password=" + password + ", sex=" + sex
                    + "]";
        }
    
    }

    jsp:

    <body>
       <form action="<%=request.getContextPath() %>/RegistServlet" method="post">
       名称:<input type="text" name="name" /><br/>
       密码:<input type="text" name="password" /><br/>
       性别:<input type="text" name="sex" /><br/>
       <input type="submit" value="提交"/>
       </form>
    </body>

    servlet:

    public class RegistServlet extends HttpServlet {
    
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    //输出bean的返回对象 System.out.println(MapToBeanUtil.toBean(request.getParameterMap(), User.
    class)); } }
  • 相关阅读:
    Win下的批处理命令
    二分查找
    Leetcode504.Base 7七进制数
    Leetcode500.Keyboard Row键盘行
    Leetcode492.Construct the Rectangle构造矩形
    Leetcode485.Max Consecutive Ones最大连续1的个数
    Leetcode475.Heaters供暖器
    hdu1233还是畅通工程
    hdu1863畅通工程
    Leetcode459.Repeated Substring Pattern重复的子字符串
  • 原文地址:https://www.cnblogs.com/sflik/p/4607702.html
Copyright © 2011-2022 走看看