BeanUtils工具

记录一下这个工具,虽然以后不一定能用到,但作为了解一下

BeanUtils工具是Apache Commons组件的成员之一,
主要用于简化JavaBean封装数据的操作。

比如在前端提交表单数据时后端接收要使用request.getParameter()
使用BeanUtils就可以直接使用如下方式,前提是表单的name属性要和
User属性名一致

@Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
    	throws ServletException, IOException {
        Map<String, String[]> param = req.getParameterMap();
        User user = new User();
        try {
            BeanUtils.populate(user,param);
            System.out.println(user);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

使用该工具需要导入的maven依赖

<!--beanUtil-->
<dependency>
    <groupId>commons-beanutils</groupId>
    <artifactId>commons-beanutils</artifactId>
    <version>1.9.3</version>
</dependency>

<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
</dependency>

该工具具体使用,请查看大佬们的博客

Apache Commons下更多组件工具,请点击跳转