Converter转换器使用
package com.xu.javabean; import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat; import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter; public class Test { // Sun公司的内省API过于繁琐,所以Apache组织结合很多实际开发中的应用场景
// 开发了一套简单、易用的API操作Bean的属性——BeanUtils,
// 在Beanutil中可以直接进行类型的自动转换。
// 使用beanUtils操作bean的属性(首先导入jar包,第三方开发工具包) @org.junit.Test
public void test1() throws Exception {
// BeanUtils可以填充JavaBeans属性通过反射实用方法。 Person p = new Person();
// void org.apache.commons.beanutils.BeanUtils
// .setProperty(Object bean, String name, Object value)
// throws IllegalAccessException, InvocationTargetException
// 设置指定的属性值,进行类型转换为所需的符合目标的属性类型。
BeanUtils.setProperty(p, "name", "zhangsan");
System.out.println(p.getName());
} @org.junit.Test
public void test2() throws IllegalAccessException,
InvocationTargetException {
// 例如:接收用户浏览器传递过来的姓名、年龄、性别等信息
// 由于是表单提交,所以都是字符串
String name = "lisi";
String age = "67";
String sex = "男";
String birthday = "1989-09-16"; // 为了让日期赋到bean的birthday属性上,我们给BeanUtils注册一个日期转换器
// ConvertUtils类转换为字符串的标量值来指定类的对象的实用方法,字符串数组来指定类的数组。 // static void register(Converter converter, Class clazz)
// 注册为指定目标类的自定义转换器,取代任何以前注册的转换器。
ConvertUtils.register(new Converter() {
// Object convert(Class type, Object value)
// 将指定的输入对象转换为指定类型的输出对象。 public Object convert(Class type, Object value) {
if (value == null) {
return null;
}
if (!(value instanceof String)) {
throw new ConversionException("只支持String类型的转换!");
}
String str = (String) value;
if (str.trim().equals("")) {
return null;
}
// SimpleDateFormat(String pattern)
// 用给定的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// Date parse(String source)
// 从给定字符串的开始解析文本,以生成一个日期。
try {
// 此方法会抛出异常,因为是匿名内部类实现接口,所以不能抛只能处理
return sdf.parse(str);
} catch (ParseException e) {
// 如果发生此类异常,那么就让程序停止,
// 必须加上参数e,因为异常链不能断,要让调用者看到异常信息
throw new RuntimeException(e);
}
}
}, Date.class); Person p = new Person();
BeanUtils.setProperty(p, "name", name);
// p对象的age属性应该接收int类型的数据,但是使用BeanUtils可以自动帮我们完成转换动作
// 只支持8种基本数据类型的转换
// 其实可以用 BeanUtils.populate(bean, properties);
BeanUtils.setProperty(p, "age", age);
BeanUtils.setProperty(p, "sex", sex);
BeanUtils.setProperty(p, "birthday", birthday); System.out.println(p.getName());
System.out.println(p.getAge());
System.out.println(p.getSex());
System.out.println(p.getBirthday());
} @org.junit.Test
public void test3() throws IllegalAccessException,
InvocationTargetException {
// 例如:接收用户浏览器传递过来的姓名、年龄、性别等信息
// 由于是表单提交,所以都是字符串
String name = "lisi";
String age = "67";
String sex = "男";
String birthday = "1989-09-16"; ConvertUtils.register(new DateLocaleConverter(), Date.class); Person p = new Person();
BeanUtils.setProperty(p, "name", name);
// p对象的age属性应该接收int类型的数据,但是使用BeanUtils可以自动帮我们完成转换动作
// 只支持8种基本数据类型的转换
BeanUtils.setProperty(p, "age", age);
BeanUtils.setProperty(p, "sex", sex);
BeanUtils.setProperty(p, "birthday", birthday); System.out.println(p.getName());
System.out.println(p.getAge());
System.out.println(p.getSex());
System.out.println(p.getBirthday());
} }
</span>
Converter转换器使用的更多相关文章
- Converter(转换器)与Formatter(格式化) ,Validator(验证器)
Converter(转换器)与Formatter(格式化)都可以用于将一种对象类型转换为另一种对象类型.Converter是通用元件,可以在应用程序的任意层中使用,而Fotermatter这是专门为W ...
- springMVC的 Converter转换器 和 Formatter
Converter转换器 spring的Converter是可以将一种类型转换成另一种类型的一个对象, 自定义Converter需要实现Converter接口 日期转换器 import java.te ...
- SpringMVC:学习笔记(6)——转换器和格式化
转换器和格式化 说明 SpringMVC的数据绑定并非没有限制,有案例表明,在SpringMVC如何正确绑定数据方面是杂乱无章的,比如在处理日期映射到Date对象上. 为了能够让SpringMVC进行 ...
- BenUtils组件和DbUtils组件
BenUtils组件和DbUtils组件 [TOC] 1.BenUtils组件 1.1.简介 程序中对javabean的操作很频繁,所有Apache提供了一套开源api,方便javabean的操作!即 ...
- .Net下一个类型转换神器
引言 类型转换经常遇到,最常用的应该是string类型转换为其它基元类型,常见于http参数类型转换.Convert静态类的Convert.ChangeType()方法可以把实现IConvertibl ...
- [转]Hibernate设置时间戳的默认值和更新时间的自动更新
原文地址:http://blog.csdn.net/sushengmiyan/article/details/50360451 Generated and default property value ...
- guava学习--集合1
Lists: 其内部使用了静态工厂方法代替构造器,提供了许多用于List子类构造和操作的静态方法,我们简单的依次进行说明,如下: newArrayList():构造一个可变的.空的ArrayList实 ...
- WPF数据绑定Binding(二)
WPF数据绑定Binding(二) 1.UI控件直接的数据绑定 UI对象间的绑定,也是最基本的形式,通常是将源对象Source的某个属性值绑定 (拷贝) 到目标对象Destination的某个属性上. ...
- Android Retrofit实现原理分析
retrofit有几个关键的地方. 1.用户自定义的接口和接口方法.(由动态代理创建对象.) 2.converter转换器.(把response转换为一个具体的对象) 3.注解的使用. 让我们跟随Ap ...
随机推荐
- 同时有background-size background-positon 两个属性的时候,如何在合并的background样式中展示
今日写css,遇到background很多属性,于是想合并写,w3c只是说了各个属性都可以合并,但是并没有给出background-size background-positon合并的具体例子 bac ...
- Autofac 同时支持MVC 与Webapi
1.引用 using Autofac; using Autofac.Integration.Mvc; using Autofac.Integration.WebApi; 2.在Global中的Appl ...
- 学习 Local Sensitive Hash
1. 最近邻法的应用 1.1 Jaccard 相似集 如何定义相似:即相关属性交集的大小,越大则越相似.我们给相似一个数学上的定义:Jaccard 相似集. 集合 \(S\) 与集合 \(T\) 的 ...
- cat /proc/devices 和ls /dev
对于新手来讲,linux的框架实在是太庞大,况且很多知识点需自己做才能理解 设备 文件 ,设备编号 #ll -a /dev 在每一行都可以看到设备文件.设备编号(主.次) 对于每种硬件设备,系统 ...
- Java和C++的虚函数的异同
参考博客:点我 要点:Java中的普通函数默认为虚函数,因此动态绑定的行为是默认的,而C++必须将方法声明为虚函数(virtual关键字),执行时才会进行动态绑定,详细区别可参考代码以及注释. 代码大 ...
- select,poll,epoll区别
select:忙轮询,一直在轮询,效率跟链接数成反比,资源限制 poll:轮询,不用一直轮询,有事件触发时轮询,资源限制 epoll:有事件触发时直接通知复杂度O(1)
- Linux 下如何安装软件
一.解析Linux应用软件安装包 通常Linux应用软件的安装包有三种: 1) tar包,如software-1.2.3-1.tar.gz.它是使用UNIX系统的打包工具tar打包的. 2) rpm包 ...
- Atom编辑器在windows下怎么更改安装路径
作为一个有良(mei)知(qian)的程序员,也不能老是用和谐版的source insight. 而且source insight也不是十分的完美,本身有一些缺陷. 比如说中文的支持,比如说反应很慢的 ...
- textarea光标处插入文字
(function($) { $.fn.extend({ //myField 对象元素 myValue 插入值 insertAtCursor: function(myField,myValue) { ...
- qsort函数、sort函数【转】
http://blog.163.com/yuhua_kui/blog/static/9679964420142195442766/ 先说明一下:qsort和sort,只能对连续内存的数据进行排序,像链 ...