apache-commons-beanutils的基本使用操作
针对于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; //文章所属的主题,如果不属于某个主题,则此值为0private 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;}}?
?