在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();
}
}
}
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 }
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>
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,