zl程序教程

您现在的位置是:首页 >  后端

当前栏目

BeanUtils.populate方法使用

方法 使用 BeanUtils populate
2023-06-13 09:12:46 时间

大家好,又见面了,我是你们的朋友全栈君。

BeanUtils.populate方法使用

1.在执行BeanUtils.populate之后,会把map封装成User对象。要注意的是,UserBean类中的字段名必须和html中的name属性值相同,不然在BeanUtils.populate执行之后,Bean对象的字段中会出现NULL数据。 该方法的函数原型为:BeanUtils.populate( Object bean, Map properties )。这个方法会遍历map<key,value>中的key,如果bean中有这个属性,就把这个key对应的value值赋给bean的属性。

Map<String, String[]> map = req.getParameterMap();
        //创建User对象
        User loginUser=new User();
        try { 
   
            BeanUtils.populate(loginUser,map);   
        } catch (IllegalAccessException e) { 
   
            e.printStackTrace();
        } catch (InvocationTargetException e) { 
   
            e.printStackTrace();
        }
<form action="/loginServlet" method="get">
    用户名:<input type="text" name="username"><br>
    密码:<input type="text" name="password"><br>
   <input type="submit" value="登录"><br>
</form>
public class User { 
   
    private String username;
    private String password;

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/158743.html原文链接:https://javaforall.cn