BeanUtil使用例子:解析并转化HttpServletRequest到Bean的全面测试
生活随笔
收集整理的這篇文章主要介紹了
BeanUtil使用例子:解析并转化HttpServletRequest到Bean的全面测试
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在Web表單提交后解析表單時,一般框架都提供了某種方式可以自動從表單映射到我們的POJO類里面。屬性會被自動填充的。
但如果我們在某個需求里,真的需要用程序來解析的話,那么如果有幾百個屬性,可就是一個噩夢了。
我們可以用java的反射機制來自己實現,也可以通過現成的輔助類庫實現。
這里我介紹的是apache的 BeanUtil庫的一個實現方式,我增強了默認的解析類,加上了日期的自定義解析。
1 先看解析類Code
package?com.laozizhu.util;
import?java.lang.reflect.InvocationTargetException;
import?java.util.Map;
import?javax.servlet.http.HttpServletRequest;
import?org.apache.commons.beanutils.BeanUtils;
import?org.apache.commons.beanutils.ConvertUtils;
import?org.apache.commons.beanutils.converters.DateConverter;
/**
?*?將HttpServletRequest解析并注入到Bean里面的輔助類。
?*
?*?@author?老紫竹研究室(laozizhu.com)
?*/
public?class?RequestToBean?{
??static?{
????DateConverter?d?=?new?DateConverter();
????String[]?datePattern?=?{?"yyyy-mm-dd",?"yyyy/mm/dd",?"yyyy.mm.dd"?};
????d.setPatterns(datePattern);
????ConvertUtils.register(d,?java.util.Date.class);
??}
??public?static?void?populate(HttpServletRequest?request,?Object?obj)?{
????Map?map?=?request.getParameterMap();
????try?{
??????BeanUtils.populate(obj,?map);
????}?catch?(IllegalAccessException?e)?{
??????e.printStackTrace();
????}?catch?(InvocationTargetException?e)?{
??????e.printStackTrace();
????}
??}
}
2 測試用的基礎類
Code
?1?package?com.laozizhu.util.test;
?2?
?3?import?java.util.Date;
?4?
?5?/**
?6??*?基礎數據類,包括常見的數據類型。
?7??*
?8??*?@author?老紫竹研究室(laozizhu.com)
?9??*/
10?public?class?Base?{
11???private?long?id;
12?
13???private?String?name;
14?
15???private?float?weight;
16?
17???private?double?height;
18?
19???private?Date?birthday;
20?
21???//?喜愛的數字
22???private?int[]?numberFavorite;
23?
24???private?boolean?married;
25?
26???public?boolean?isMarried()?{
27?????return?married;
28???}
29?
30???public?void?setMarried(boolean?married)?{
31?????this.married?=?married;
32???}
33?
34???public?long?getId()?{
35?????return?id;
36???}
37?
38???public?void?setId(long?id)?{
39?????this.id?=?id;
40???}
41?
42???public?String?getName()?{
43?????return?name;
44???}
45?
46???public?void?setName(String?name)?{
47?????this.name?=?name;
48???}
49?
50???public?Date?getBirthday()?{
51?????return?birthday;
52???}
53?
54???public?void?setBirthday(Date?birthday)?{
55?????this.birthday?=?birthday;
56???}
57?
58???public?float?getWeight()?{
59?????return?weight;
60???}
61?
62???public?void?setWeight(float?weight)?{
63?????this.weight?=?weight;
64???}
65?
66???public?double?getHeight()?{
67?????return?height;
68???}
69?
70???public?void?setHeight(double?height)?{
71?????this.height?=?height;
72???}
73?
74???public?int[]?getNumberFavorite()?{
75?????return?numberFavorite;
76???}
77?
78???public?void?setNumberFavorite(int[]?numberFavorite)?{
79?????this.numberFavorite?=?numberFavorite;
80???}
81?}
3 測試用的JSP代碼
Code
?1?<%@page?c%>
?2?<%@page?import="com.laozizhu.util.*,com.laozizhu.util.test.*"%>
?3?<%
?4???String?title?=?"老紫竹研究室類庫演示程序-基礎測試:整數、浮點數、字符串、日期、多選數字、布爾";
?5?%>
?6?<html>
?7?<head>
?8?<title><%=title%></title>
?9?</head>
10?<body>
11?<%
12???request.setCharacterEncoding("UTF-8");
13???Base?b?=?new?Base();
14???RequestToBean.populate(request,?b);
15?%>
16?<%=title%>
17?<br?/>
18?編號:<%=b.getId()%><br?/>
19?名字:<%=b.getName()%><br?/>
20?身高:<%=b.getHeight()%><br?/>
21?體重:<%=b.getWeight()%><br?/>
22?生日:<%=b.getBirthday()%><br?/>
23?婚否:<%=b.isMarried()%><br?/>
24?數字:<%
25???if?(b.getNumberFavorite()?!=?null)
26?????for?(int?num?:?b.getNumberFavorite())?{
27???????out.print(num?+?",");
28?????}
29?%>
30?<form?method="post"><br?/>
31?編號:<input?type="text"?name="id"?value="1"?/><br?/>
32?名字:<input?type="text"?name="name"?value="老紫竹"?/><br?/>
33?身高:<input?type="text"?name="height"?value="173.5"?/><br?/>
34?體重:<input?type="text"?name="weight"?value="90.3"?/><br?/>
35?生日:<input?type="text"?name="birthday"?value="2009-01-23"?/><br?/>
36?婚否:<input?type="radio"?name="married"?value="1"?checked?/>已婚,&?lt;input?type="radio"?name="married"?value="0"?/>未婚<br?/>
37?數字:<br?/>
38?<%
39???for?(int?i?=?0;?i?<=?9;?i++)?{
40?%>?<input?type="checkbox"?name="numberFavorite"?value="<%=i?%>"?<%=i%3==0?"?checked":""?%>?/><%=i%><br?/>
41?<%
42???}
43?%>?<input?type="submit"?value="提交測試"?/><br?/>
44?</form>
45?</body>
46?</html>
4 運行結果
老紫竹研究室類庫演示程序-基礎測試:整數、浮點數、字符串、日期、多選數字
編號:1
名字:老紫竹
身高:173.5
體重:90.3
生日:Fri Jan 23 00:01:00 CST 2009
婚否:true
數字:0,3,6,9,
轉載于:https://www.cnblogs.com/laozizhu/archive/2009/03/27/1422813.html
總結
以上是生活随笔為你收集整理的BeanUtil使用例子:解析并转化HttpServletRequest到Bean的全面测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php 查看本机ip,查看本机ip地址用
- 下一篇: Sprd平台添加Camera驱动(SL8