文章目錄
- apache BeanUtils工具類簡介
- BeanUtils主要方法
- BeanUtils的環境配置
- 使用BeanUtils第三方工具,需要匯入jar包:
- 在WEB-INF下創建lib目錄,將jar包匯入
- 選定jar包通過add As Library將外部jar包添加到idea專案中
- 添加成功可以通過`ctrl+alt+shift+s`快捷鍵打開Project Structrue的Libraries查看和洗掉
- 接下來就是看用BeanUtils和不用的區別了
- 共同頁面
- 創建bean類User物件
- 撰寫servlet -- 沒有用BeanUtils工具寫
- 呼叫BeanUtils的populate()方法實作
- 最終顯示效果
- 個人總結
apache BeanUtils工具類簡介
- BeanUtils是什么?
BeanUtils 是 Apache commons組件的成員之一 - 有什么用?
主要用于簡化JavaBean封裝資料的操作,
BeanUtils主要方法
| 方法 | 描述 |
|---|---|
| populate(Object bean, Map<String,String[]>properties) | 將Map資料封裝到指定Javabean中,一般用于將表單的所有資料封裝到javabean |
| setProperty(Object obj,String name,Object value) | 設定屬性值 |
| getProperty(Object obj,String name) | 獲得屬性值 |
BeanUtils的環境配置
使用BeanUtils第三方工具,需要匯入jar包:
在WEB-INF下創建lib目錄,將jar包匯入

選定jar包通過add As Library將外部jar包添加到idea專案中


添加成功可以通過ctrl+alt+shift+s快捷鍵打開Project Structrue的Libraries查看和洗掉


接下來就是看用BeanUtils和不用的區別了
共同頁面
<html>
<head>
<title>登錄界面</title>
</head>
<body>
<h3>post請求</h3>
<form action="body" method="post">
<table>
<tr>
<td>賬號:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密碼:</td>
<td><input type="password" name="password"></td>
</tr>
<tr align="center"><input type="submit" value="登錄"></tr>
<tr>
<td>愛好:</td>
<td>
<input type="checkbox" name="hobbys" value="敲代碼"/>敲代碼
<input type="checkbox" name="hobbys" value="寫博客"/>寫博客
<input type="checkbox" name="hobbys" value="看底層"/>看底層
<input type="checkbox" name="hobbys" value="學操作"/>學操作
</td>
</tr>
<tr>
<td>性別:</td>
<td>
<input type="radio" value="男" name="sex"/>男
<input type="radio" value="女" name="sex"/>女
</td>
</tr>
</table>
</form>
</body>
</html>
創建bean類User物件
這里的User物件的屬性名和頁面對應的name值要一致,因為BeanUtils通過反射實作,通過get、set方法,如果名稱不一樣會找不到
public class User {
//使用BeanUtils需要這個屬性名和頁面對應輸入的name屬性名一致
private String username;
private String password;
private String[] hobbys;
private String sex;
//省略構造方法 getter、setter方法
}
撰寫servlet – 沒有用BeanUtils工具寫
@WebServlet("/BeanutilsServlet")
public class BeanutilsServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//沒有使用BeanUtils ,寫得很麻煩 ,一個一個地獲取,再一個一個的設定
//設定編碼
request.setCharacterEncoding("UTF-8");
User user = new User();
//1.獲取所有的引數
Map<String, String[]> map = request.getParameterMap();
//2.賦值給一個User物件
String[] usernames = map.get("username");
user.setUsername(usernames[0]);
String[] passwords = map.get("password");
user.setPassword(passwords[0]);
String[] hobbies = map.get("hobbys");
user.setHobbys(hobbies);
String[] sexes = map.get("sex");
user.setSex(sexes[0]);
//將user物件顯示到瀏覽器上
System.out.println(user);
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(user.toString().getBytes());
}
}
呼叫BeanUtils的populate()方法實作
而如果用Beanutils就非常簡單了,它幫我們自動封裝類,就是bean類對應的屬性名和頁面輸入的name值要一對一對應
@WebServlet("/BeanutilsServlet")
public class BeanutilsServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//沒有使用BeanUtils ,寫得很麻煩 ,一個一個地獲取,再一個一個的設定
//設定編碼
request.setCharacterEncoding("UTF-8");
User user = new User();
//獲取所有的引數
Map<String, String[]> map = request.getParameterMap();
try {
//參1、對應的javabean物件 參2 從瀏覽器讀取到的引數key-value的map值
BeanUtils.populate(user,map);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(user);
//將user物件顯示到瀏覽器上
System.out.println(user);
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(user.toString().getBytes());
}
}
最終顯示效果


個人總結
BeanUtils真的是一個非常好用的東西,他直接將表單的物件封裝成bean供我們使用,特別是對于那種一堆屬性的bean來說,它極大的減少了我們的開發代碼量,
別看我現在寫的好像減少的代碼不多,但是如果是那種大量屬性的(比如:醫院病歷單等等大量屬性)的bean來說,這樣可以大量減少我們開發量,
注意:這里面的bean對應的屬性名要與頁面的name值對應,不然BeanUtils通過反射的get、set會找不到對應的屬性值
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/11683.html
標籤:其他
上一篇:突然報 _vm.tap1 is not a function
下一篇:裂紋連接
