BeanUtils入门
生活随笔
收集整理的這篇文章主要介紹了
BeanUtils入门
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
BeanUtils入門
基本概述
BeanUtils 是 Apache commons組件的成員之一,主要用于簡化JavaBean封裝數(shù)據(jù)的操作。它可以給JavaBean封裝一個(gè)字符串?dāng)?shù)據(jù),也可以將一個(gè)表單提交的所有數(shù)據(jù)封裝到JavaBean中。
PS:下載地址:https://github.com/apache/commons-beanutils/
常用API
BeanUtils工具常用工具類有兩個(gè):BeanUtils、ConvertUtils。BeanUtils用于封裝數(shù)據(jù),ConvertUtils用于處理類型轉(zhuǎn)換,常用API如下:
| BeanUtils對象 | populate(Object bean, Map<String,String[]> properties) | 將Map數(shù)據(jù)封裝到指定Javabean中,一般用于將表單的所有數(shù)據(jù)封裝到j(luò)avabean |
| ConvertUtils | register(Converter converter, Class clazz) | 注冊類型轉(zhuǎn)換器 |
封裝表單提交基本步驟
創(chuàng)建JavaBean
package com.pc.domain;import java.util.Date;/*** 用戶Bean* * @author Switch* @data 2016年10月11日* @version V1.0*/ public class User {private Integer id;private String userName;private String password;private String sex;private String hobby;private Date birthday;// 無參構(gòu)造方法public User() {}// 設(shè)置器和獲取器public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}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;}public String getHobby() {return hobby;}public void setHobby(String hobby) {this.hobby = hobby;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}@Overridepublic String toString() {return "User [id=" + id + ", userName=" + userName + ", password=" + password + ", sex=" + sex + ", hobby="+ hobby + ", birthday=" + birthday + "]";}}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
Servlet獲取表單數(shù)據(jù)
表單
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html><head><meta charset=UTF-8"><title>注冊頁面2</title></head><body><div align="center" style="position: relative; top: 100px;"><form action="TestBeanServlet" method="post">用戶名:<input type="text" name="userName" /><br />密碼:<input type="password" name="password" /><br />性別:<input type="radio" name="sex" value="男" />男<input type="radio" name="usex" value="女" />女<br />愛好:<input type="checkbox" name="hobby" value="蹦迪" />蹦迪<input type="checkbox" name="hobby" value="攀巖" />攀巖<input type="checkbox" name="hobby" value="蹦極" />蹦極<br />生日:<input type="text" name="birthday" /><br/><input type="submit" value="注冊" /></form></div></body> </html>- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
獲取表單數(shù)據(jù)
package com.pc.beanutils;import java.io.IOException; import java.util.Map;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;/*** 測試BeanUtils工具* * @author Switch* @data 2016年10月11日* @version V1.0*/ public class TestBeanServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");request.setCharacterEncoding("utf-8");// 獲取表單數(shù)據(jù)集Map<String, String[]> formMap = request.getParameterMap();}public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);} }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
編寫工具類
package com.pc.utils;import java.util.Date; import java.util.Map;import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.converters.DateConverter;/*** Bean封裝工具類* * @author Switch* @data 2016年10月11日* @version V1.0*/ public class MyBeanUtils {public static <T> T populate(Class<T> beanClass, Map<String, String[]> props) throws Exception {// 使用反射創(chuàng)建實(shí)例T bean = beanClass.newInstance();// 創(chuàng)建BeanUtils提供的時(shí)間轉(zhuǎn)換器DateConverter dateConverter = new DateConverter();// 設(shè)置轉(zhuǎn)換格式dateConverter.setPatterns(new String[] { "yyyy-MM-dd", "yyyy/MM/dd", "yyyyMMdd" });// 注冊轉(zhuǎn)換器ConvertUtils.register(dateConverter, Date.class);// 封裝數(shù)據(jù)BeanUtils.populate(bean, props);return bean;} }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
測試轉(zhuǎn)換工具
package com.pc.beanutils;import java.io.IOException; import java.util.Arrays; import java.util.Map;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import com.pc.domain.User; import com.pc.utils.MyBeanUtils;/*** 測試BeanUtils工具* * @author Switch* @data 2016年10月11日* @version V1.0*/ public class TestBeanServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {try {response.setContentType("text/html;charset=utf-8");request.setCharacterEncoding("utf-8");// 獲取表單數(shù)據(jù)集Map<String, String[]> formMap = request.getParameterMap();// 封裝數(shù)據(jù)User user = MyBeanUtils.populate(User.class, formMap);// 處理特殊數(shù)據(jù)類型String[] hobbies = request.getParameterValues("hobby");// 將其轉(zhuǎn)換為hobby1,hobby2,hobby3這樣的StringString hobby = Arrays.toString(hobbies).substring(1, Arrays.toString(hobbies).length() - 1);user.setHobby(hobby);System.out.println(user);} catch (Exception e) {e.printStackTrace();}}public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);} }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
測試
PS:GitHub:https://github.com/Switch-vov/servlet-request-test
from:?http://blog.csdn.net/q547550831/article/details/52791671
總結(jié)
以上是生活随笔為你收集整理的BeanUtils入门的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java动态代理类使用
- 下一篇: ServletJSP学习笔记--导航