这个东西在许多框架上已经内置实现了,只不过我还是对这个有兴趣,所以有重新测试了一下。
首发地址: http://www.laozizhu.com/view.jsp?articleId=103
我增强了默认的解析类,加上了日期的自定义解析,
1 先看解析类
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 测试用的基础类
package com.laozizhu.util.test;
import java.util.Date;
/**
* 基础数据类,包括常见的数据类型。
*
* @author 老紫竹研究室(laozizhu.com)
*/
public class Base {
private long id;
private String name;
private float weight;
private double height;
private Date birthday;
// 喜爱的数字
private int[] numberFavorite;
private boolean married;
public boolean isMarried() {
return married;
}
public void setMarried(boolean married) {
this.married = married;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public int[] getNumberFavorite() {
return numberFavorite;
}
public void setNumberFavorite(int[] numberFavorite) {
this.numberFavorite = numberFavorite;
}
}
3 测试用的JSP代码
<%@page c%>
<%@page import="com.laozizhu.util.*,com.laozizhu.util.test.*"%>
<%
String title = "老紫竹研究室类库演示程序-基础测试:整数、浮点数、字符串、日期、多选数字、布尔";
%>
<html>
<head>
<title><%=title%></title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
Base b = new Base();
RequestToBean.populate(request, b);
%>
<%=title%>
<br />
编号:<%=b.getId()%><br />
名字:<%=b.getName()%><br />
身高:<%=b.getHeight()%><br />
体重:<%=b.getWeight()%><br />
生日:<%=b.getBirthday()%><br />
婚否:<%=b.isMarried()%><br />
数字:<%
if (b.getNumberFavorite() != null)
for (int num : b.getNumberFavorite()) {
out.print(num + ",");
}
%>
<form method="post"><br />
编号:<input type="text" name="id" value="1" /><br />
名字:<input type="text" name="name" value="老紫竹" /><br />
身高:<input type="text" name="height" value="173.5" /><br />
体重:<input type="text" name="weight" value="90.3" /><br />
生日:<input type="text" name="birthday" value="2009-01-23" /><br />
婚否:<input type="radio" name="married" value="1" checked />已婚,& lt;input type="radio" name="married" value="0" />未婚<br />
数字:<br />
<%
for (int i = 0; i <= 9; i++) {
%> <input type="checkbox" name="numberFavorite" value="<%=i %>" <%=i%3==0?" checked":"" %> /><%=i%><br />
<%
}
%> <input type="submit" value="提交测试" /><br />
</form>
</body>
</html>
4 运行结果
老紫竹研究室类库演示程序-基础测试:整数、浮点数、字符串、日期、多选数字
编号:1
名字:老紫竹
身高:173.5
体重:90.3
生日:Fri Jan 23 00:01:00 CST 2009
婚否:true
数字:0,3,6,9,