• 官网地址:http://commons.apache.org/proper/commons-beanutils/

    • org.apache.commons.beanutils.BeanUtils

    • 将HttpServletRequest中的参数封装成Bean类 ``` @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    1. User loginUser = new User();
    2. try {
    3. Map<String, String[]> parameterMap = req.getParameterMap();
    4. BeanUtils.populate(loginUser,parameterMap);
    5. } catch (IllegalAccessException e) {
    6. e.printStackTrace();
    7. } catch (InvocationTargetException e) {
    8. e.printStackTrace();
    9. }
    10. }
    1. - 原理
    2. > BeanUtils.setProperty(user,"username","张三");
    3. >
    4. > BeanUtils.getProperty(user, "username");
    1. User user = new User();
    2. try {
    3. // 会去找setUsername方法设置值
    4. BeanUtils.setProperty(user,"username","张三");
    5. // 会去找getUsername方法取值
    6. String username = BeanUtils.getProperty(user, "username");
    7. System.out.println(username);
    8. } catch (IllegalAccessException e) {
    9. e.printStackTrace();
    10. } catch (InvocationTargetException e) {
    11. e.printStackTrace();
    12. } catch (NoSuchMethodException e) {
    13. e.printStackTrace();
    14. }
    15. // User{id=0, username='张三', password='null'}
    16. System.out.println(user.toString());

    ```