今日學習目標 :
- 可以說出BeanUtils的做用
- 可以使用BeanUtils封裝數據
- 可以編寫XML綜合案例
- BeanUtils (*****瞭解*****)
JavaBean 介紹 :java
JavaBean 它是一個簡單的Java類, 這個類擁有 get 和 set 方法便可. 但要求這個類必須有一個公開的空參構造方法.apache
後期開發中, 這個類通常會保存在名爲 domain 或者 beans 包中.dom
BeanUtils 介紹 :ide
在 Servlet 中通常須要獲取頁面提交的form表單中的數據, 而後封裝到一個JavaBean上.工具
這樣咱們須要書寫大量的 getParameter 獲取提交的參數, 同時還要使用 JavaBean 的 setParameter 方法把數據所有封裝到對象上.學習
因而 Apache 公司給咱們提供了能夠快速獲取頁面數據, 並將數據封裝到JavaBean對象上的方式.this
BeanUtils 類的功能是把一個Map集合中的數據, 封裝到一個 JavaBean 上. 使用BeanUtils工具類中的populate方法就能夠把Map集合中的數據封裝到指定的bean上.Map中的key名稱必須和bean對象的屬性名稱保持一致.spa
Request 請求 -> getParameter -> getParameterMap(); -> populate 填充 -> JavaBean 類的對象..net
![](http://static.javashuo.com/static/loading.gif)
BeanUtils 做用 :orm
能夠爲JavaBean的屬性設置值, 獲取值, 並經過Map爲JavaBean的數值賦值.
連接 : http://commons.apache.org/proper/commons-beanutils/download_beanutils.cgi
連接 : http://commons.apache.org/proper/commons-logging/download_logging.cgi
BeanUtils 是 Apache commons組件的成員之一,主要用於簡化JavaBean封裝數據的操做。它能夠給JavaBean封裝一個字符串數據,也能夠將一個表單提交的全部數據封裝到JavaBean中。
使用第三方工具,須要導入jar包:
![](http://static.javashuo.com/static/loading.gif)
|
方法 |
描述 |
BeanUtils對象 |
populate(Object bean, Map<String,String[]> properties) |
將Map數據封裝到指定Javabean中,通常用於將表單的全部數據封裝到javabean |
setProperty(Object obj,String name,Object value) |
設置屬性值 |
getProperty(Object obj,String name) |
得到屬性值 |
- 提供JavaBean User ,並提供對應的構造方法
- package cn.itcast.domain;
-
- public class User {
- // 屬性
- private String id;
- private String username;
- private String pwd;
- private int age;
- private float scores;
-
- public User(String id, String username, String pwd, int age, float scores) {
- super();
- this.id = id;
- this.username = username;
- this.pwd = pwd;
- this.age = age;
- this.scores = scores;
- }
-
- public User() {
- super();
- }
-
- @Override
- public String toString() {
- return "User [id=" + id + ", username=" + username + ", pwd=" + pwd + ", age=" + age + ", scores=" + scores
- + "]";
- }
- public int getAge() {
- return age;
- }
- public float getScores() {
- return scores;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public void setScores(float scores) {
- this.scores = scores;
- }
- public String getId() {
- return id;
- }
- public String getUsername() {
- return username;
- }
- public String getPwd() {
- return pwd;
- }
- public void setId(String id) {
- this.id = id;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public void setPwd(String pwd) {
- this.pwd = pwd;
- }
- }
- 功能1:設置屬性
- 功能2:得到屬性
- import org.apache.commons.beanutils.BeanUtils;
- import org.junit.Test;
-
- public class BeanUtilsDemo01 {
- @Test
- public void demo01() throws Exception {
- User user = new User();
-
- // 設置
- BeanUtils.setProperty(user, "id", "u001");
- BeanUtils.setProperty(user, "username", "Jack");
- BeanUtils.setProperty(user, "pwd", "123456");
- BeanUtils.setProperty(user, "age", 18);
- BeanUtils.setProperty(user, "scores", 99.5f);
-
- System.out.println(user);
- }
-
- @Test
- public void demo02() throws Exception {
- User user = new User("u001", "Jack", "123456", 18, 99.5f);
-
- String id = BeanUtils.getProperty(user, "id");
- String username = BeanUtils.getProperty(user, "username");
- String pwd = BeanUtils.getProperty(user, "pwd");
- String age = BeanUtils.getProperty(user, "age");
- String scores = BeanUtils.getProperty(user, "scores");
-
- System.out.println(id + ", " + username + ", " + pwd + ", " + age + ", " + scores);
- }
- }
- 功能3:封裝表單數據,使用Map 模擬 request.getParameterMap()
![](http://static.javashuo.com/static/loading.gif)
- import java.util.HashMap;
- import java.util.Map;
-
- import org.apache.commons.beanutils.BeanUtils;
-
- public class BeanUtilsDemo02 {
- public static void main(String[] args) throws Exception {
-
- // 功能3 : 封裝表單數據, 使用 Map 模擬 request.getParameterMap();
- // 需求 : 將數據封裝到 JavaBean 對象中
- User user = new User();
- BeanUtils.populate(user, Request.getParameterMap());
- System.out.println(user);
- }
- }
-
- class Request {
- // 方法
- public static Map<String, Object> getParameterMap() {
- HashMap<String, Object> map = new HashMap<String, Object>();
- map.put("id", "u001");
- map.put("username", "Jack");
- map.put("pwd", "123456");
- map.put("age", 18);
- map.put("scores", 99.5f);
- return map;
- }
- }
![](http://static.javashuo.com/static/loading.gif)
- 綜合案例1 (*****練習*****)
讀取XML中的配置文件信息,使用BeanUtils工具類建立JavaBean對象,將XML中的數據保存到JavaBean類的屬性中.
![](http://static.javashuo.com/static/loading.gif)
xml文件 :
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean className="cn.itcast.bean.Employee">
<property name="id" value="1"></property>
<property name="name" value="張三"></property>
<property name="salary" value="20000.00"></property>
</bean>
<bean className="cn.itcast.bean.Student">
<property name="name" value="翠花"></property>
<property name="age" value="18"></property>
<property name="gender" value="女"></property>
<property name="score" value="99.5f"></property>
</bean>
</beans>
建立兩個 JavaBean 類 :
- package cn.itcast.domain;
-
- public class Employee {
- // 屬性
- private int id;
- private String name;
- private double salary;
-
- // 構造方法重載
- public Employee(int id, String name, double salary) {
- super();
- this.id = id;
- this.name = name;
- this.salary = salary;
- }
-
- @Override
- public String toString() {
- return "Employee [id=" + id + ", name=" + name + ", salary=" + salary + "]";
- }
-
- // setter & getter
- public Employee() {
- super();
- }
- public int getId() {
- return id;
- }
- public String getName() {
- return name;
- }
-
- public double getSalary() {
- return salary;
- }
-
- public void setId(int id) {
- this.id = id;
- }
- public void setName(String name) {
- this.name = name;
- }
- public void setSalary(double salary) {
- this.salary = salary;
- }
- }
- package cn.itcast.domain;
-
- public class Student {
- // 屬性
- private String name;
- private int age;
- private char gender;
- private float score;
- public Student(String name, int age, char gender, float score) {
- super();
- this.name = name;
- this.age = age;
- this.gender = gender;
- this.score = score;
- }
- public Student() {
- super();
- }
- @Override
- public String toString() {
- return "Student [name=" + name + ", age=" + age + ", gender=" + gender + ", score=" + score + "]";
- }
- public String getName() {
- return name;
- }
- public int getAge() {
- return age;
- }
- public char getGender() {
- return gender;
- }
- public float getScore() {
- return score;
- }
- public void setName(String name) {
- this.name = name;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public void setGender(char gender) {
- this.gender = gender;
- }
- public void setScore(float score) {
- this.score = score;
- }
- }
解析 xml 並實現爲 JavaBean 對象賦值 :
- import java.util.List;
-
- import org.apache.commons.beanutils.BeanUtils;
- import org.dom4j.Document;
- import org.dom4j.Element;
- import org.dom4j.io.SAXReader;
-
- @SuppressWarnings("all")
- public class BeanUtilsDemo03 {
- public static void main(String[] args) throws Exception {
-
- // 1. 建立一個 SAXReader 解析器
- SAXReader saxReader = new SAXReader();
- // 2. 讀取 xml 文檔, 獲取 docuemnt 對象
- Document document = saxReader.read("employee.xml");
- // 3. 獲取根標籤
- Element rootElement = document.getRootElement();
- // 4. 獲取beans標籤下的子標籤bean
- List<Element> beanElements = rootElement.elements();
- // 5.1 遍歷集合, 獲取集合中的 bean 標籤
- for (Element bean : beanElements) {
- // 5.2 獲取bean標籤中的className屬性值
- String className = bean.attributeValue("className");
- // System.out.println(className);
- // 5.3 根據className字符串, 使用反射技術, 建立一個該類的實例對象
- Class<?> cls = Class.forName(className);
- Object obj = cls.newInstance();
- // 5.4 獲取 bean 標籤的 property 標籤
- List<Element> propElements = bean.elements();
- // 5.5 遍歷集合, 獲取集合中的每個 property 標籤
- for (Element prop : propElements) {
- // 5.6 獲取 prop 標籤中的 name 和 value 屬性值
- String name = prop.attributeValue("name");
- String value = prop.attributeValue("value");
- // System.out.println(name + " = " + value);
- // 5.7 使用 BeanUtils 工具給對象的相應屬性設置數值
- BeanUtils.setProperty(obj, name, value);
- }
- System.out.println(obj);
- }
- }
- }
![](http://static.javashuo.com/static/loading.gif)
- 綜合案例2 (*****練習*****)
student.xml
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student stuNo="it001">
<name>章子怡</name>
<age>20</age>
<gender>女</gender>
<score>99</score>
</student>
<student stuNo="it002">
<name>汪峯</name>
<age>22</age>
<gender>男</gender>
<score>100</score>
</student>
<student stuNo="it003">
<name>高圓圓</name>
<age>18</age>
<gender>女</gender>
<score>98</score>
</student>
</students>
Student.java
- public class Student {
- // 屬性
- private String stuNo;
- private String name;
- private int age;
- private char gender;
- private float score;
- public Student(String stuNo, String name, int age, char gender, float score) {
- super();
- this.stuNo = stuNo;
- this.name = name;
- this.age = age;
- this.gender = gender;
- this.score = score;
- }
- public Student() {
- super();
- }
- @Override
- public String toString() {
- return "Student [stuNo=" + stuNo + ", name=" + name + ", age=" + age + ", gender=" + gender + ", score=" + score
- + "]";
- }
- public String getStuNo() {
- return stuNo;
- }
- public String getName() {
- return name;
- }
- public int getAge() {
- return age;
- }
- public char getGender() {
- return gender;
- }
- public float getScore() {
- return score;
- }
- public void setStuNo(String stuNo) {
- this.stuNo = stuNo;
- }
- public void setName(String name) {
- this.name = name;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public void setGender(char gender) {
- this.gender = gender;
- }
- public void setScore(float score) {
- this.score = score;
- }
- }
StudentDemo01.java
- import java.util.ArrayList;
- import java.util.List;
-
- import org.apache.commons.beanutils.BeanUtils;
- import org.dom4j.Document;
- import org.dom4j.Element;
- import org.dom4j.io.SAXReader;
-
- @SuppressWarnings("all")
- public class StudentDemo01 {
- public static void main(String[] args) throws Exception {
- // 建立一個集合, 存儲 Student 類型的對象
- ArrayList<Student> list = new ArrayList<Student>();
-
- // 解析 xml
- SAXReader saxReader = new SAXReader();
- Document document = saxReader.read("student.xml");
- Element rootElement = document.getRootElement();
- List<Element> stuElements = rootElement.elements();
- for (Element stu : stuElements) {
- // 獲取 stuNo 的屬性值
- String stuNo = stu.attributeValue("stuNo");
- System.out.println(stuNo);
- // 建立一個學生對象, 併爲學生對象的 stuNo 的屬性賦值
- Student s = new Student();
- s.setStuNo(stuNo);
- // 再獲取 stu 標籤下的全部子標籤
- List<Element> elements = stu.elements();
- for (Element element : elements) {
- String name = element.getName(); // 獲取標籤名
- String value = element.getText(); // 獲取標籤中的文本信息
- System.out.println("\t" + name + " = " + value);
- // 將 name 和 value 賦值給建立的學生對象
- BeanUtils.setProperty(s, name, value);
- }
- // 將建立的 Student 對象添加到集合中
- list.add(s);
- }
-
- // 遍歷查看集合
- for (Student stu : list) {
- System.out.println(stu);
- }
- }
- }
輸出結果 :
![](http://static.javashuo.com/static/loading.gif)