针对于servlet+jsp的web项目的话,我们在将页面上传过来的值,在传入对象中去的时候,会很麻烦,要一个一个的set进去,而且还要注意格式的转换,下面apache-commons-beanutils 就可以很好的解决这个问题。
bean类:
import java.util.Date;
import java.util.Set;
public class Article {
private int id;
private String title;//标题
private String content;//内容
private String source;//来源
private String author; //作者
private String keyword; //关键字
private String intro; //简介
private String type; //分类
private boolean recommend; //是否推荐阅读
private boolean headline; //是否作为首页头条
private int leaveNumber; //留言数
private int clickNumber; //点击量
private Set<Channel> channels; //所属频道
private int topicId; //文章所属的主题,如果不属于某个主题,则此值为0
private Date createTime; //创建时间
private Date updateTime; //更新时间
private Date deployTime; //发布时间
private int adminId; //本篇文章是由哪个管理员创建的
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public Date getDeployTime() {
return deployTime;
}
public void setDeployTime(Date deployTime) {
this.deployTime = deployTime;
}
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
public String getIntro() {
return intro;
}
public void setIntro(String intro) {
this.intro = intro;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getLeaveNumber() {
return leaveNumber;
}
public void setLeaveNumber(int leaveNumber) {
this.leaveNumber = leaveNumber;
}
public int getClickNumber() {
return clickNumber;
}
public void setClickNumber(int clickNumber) {
this.clickNumber = clickNumber;
}
public Set<Channel> getChannels() {
return channels;
}
public void setChannels(Set<Channel> channels) {
this.channels = channels;
}
public int getAdminId() {
return adminId;
}
public void setAdminId(int adminId) {
this.adminId = adminId;
}
public int getTopicId() {
return topicId;
}
public void setTopicId(int topicId) {
this.topicId = topicId;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public boolean isRecommend() {
return recommend;
}
public void setRecommend(boolean recommend) {
this.recommend = recommend;
}
public boolean isHeadline() {
return headline;
}
public void setHeadline(boolean headline) {
this.headline = headline;
}
}
2 BeanUtilstest写的测试类:
import java.util.Date;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.junit.Test;
import junit.framework.TestCase;
public class BeanUtilstest extends TestCase{
public void testBeanUtils01() throws Exception
{
Article art=new Article();
BeanUtils.copyProperty(art, "title", "标题");
System.out.println(art.getTitle());
}
public void testBeanUtils02() throws Exception
{
Article art=new Article();
BeanUtils.copyProperty(art, "leaveNumber", "22");
System.out.println(art.getLeaveNumber());
}
public void testBeanUtils03() throws Exception
{
Article art=new Article();
BeanUtils.copyProperty(art, "recommend", "true");
System.out.println(art.isRecommend());
}
public void testBeanUtils04() throws Exception
{
Article art=new Article();
//注册你自定义的时间格式转换器
ConvertUtils.register(new DateConverter(), Date.class);
BeanUtils.copyProperty(art, "createTime", "2010-09-11");
//这里打印时间的 则会报java.lang.IllegalArgumentException: argument type mismatch错误
//各个国家的时间格式不一样,所以apache无法去规范规范每个国家的日期格式,这个日期的话 你就只能自己去定义
//自己去定义一个时间转换器, 很简单 就是去实现一个Converter接口
System.out.println(art.getCreateTime());
}
}
3 在处理时间格式上 DateConverter.java
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.beanutils.Converter;
public class DateConverter implements Converter {
private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
/**
* 要将字符串的value转换为java.util.Date类型的值
* targetClass 第一个参数 是你的目标类型
* value 要转换的值,
* Object 把要转回的值,返回出去就ok了
*/
public Object convert(Class targetClass, Object value) {
if (targetClass != Date.class) {
return null;
}
try {
if (value instanceof String) {
String v = (String) value;
return format.parse(v);
}
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}