BeanUtil解析HttpServletRequest到Bean的测试
来源:互联网 发布:av淘宝最新发布网站 编辑:程序博客网 时间:2024/05/16 14:36
这个东西在许多框架上已经内置实现了,只不过我还是对这个有兴趣,所以有重新测试了一下。
首发地址: 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 />已婚,<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,
- BeanUtil解析HttpServletRequest到Bean的测试
- Spring 解析httpServletRequest注入参数到Bean的拦截包装
- BeanUtil 操作Bean属性
- HttpServletRequest的部分方法解析
- BeanUtil工具包的使用
- beanutil常用的方法
- BeanUtil
- BeanUtil
- BeanUtil
- BeanUtil
- BeanUtil
- BeanUtil
- BeanUtil
- beanutil
- xstream 解析xml时只取想要的数据到bean
- 使用Gson解析json到任意类型的Bean
- commons-beanutil工具的使用
- BeanUtil的应用填充数据
- 光明网 静态模板开发
- 上网本动了谁的奶酪
- 09.1.18
- Waternie的专栏新开张
- NAND和NOR flash的区别(转载)
- BeanUtil解析HttpServletRequest到Bean的测试
- 自己的osgi收藏(比较详尽的osgi学习文档)二
- ubuntu的两个基础问题解决方案
- BeanUtils使用:ConvertUtils注册多个日期的解析格式
- 自己的osgi收藏(比较详尽的osgi学习文档)三
- 关于asp.net 代码显示在浏览器里的一点疑问
- 自己的osgi收藏(比较详尽的osgi学习文档)四spring-osgi集成
- ubuntu8.10下安装QQ for Linux
- 每日IN语(2009-01-18)优点的错误是找不到缺点,缺点的错误是