请使用beanUitls框架操作bean的属性,然后会自定义转换器
来源:互联网 发布:搜狐媒体大厦淘宝地址 编辑:程序博客网 时间:2024/06/07 20:46
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
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;
import org.junit.Test;
public class Demo1 {
@Test
public void test1() throws IllegalAccessException, InvocationTargetException{
Person p=new Person();
BeanUtils.setProperty(p,"name","张三");
System.out.println(p.getName());
}
@Test
public void test2() throws IllegalAccessException, InvocationTargetException{
//自定义转换器
ConvertUtils.register(new Converter(){
@Override
public Object convert(Class type, Object value) {
if(value==null){
return null;
}
if(!(value instanceof String)){
throw new ConversionException("只能转String数据");
}
String s=(String)value;
if(s.trim().equals("")){
return null;
}
SimpleDateFormat sdf=new SimpleDateFormat("2012-9-18");
try {
Date d=sdf.parse(s);
return d;
} catch (ParseException e) {
// TODO Auto-generated catch block
throw new ConversionException("转换错误");
}
}
}, Date.class);
String name="刘毅杰";
String age="20";
String birthday="19930226";
Person p=new Person();
BeanUtils.setProperty(p,"name",name);
BeanUtils.setProperty(p,"age",age);
BeanUtils.setProperty(p,"birthday",birthday);
System.out.println(p.getName()+"..."+p.getAge()+"...");
}
@Test
public void test3() throws IllegalAccessException, InvocationTargetException{
ConvertUtils.register(new DateLocaleConverter(), Date.class);
Person p=new Person();
BeanUtils.setProperty(p,"birthday"," ");
}
}
package com.hbsi.introspector;
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAb(){
return null;
}
}
package com.hbsi.introspector;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.junit.Test;
public class Demo1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
@Test
public void test1() throws Exception{
BeanInfo bi=Introspector.getBeanInfo(Person.class,Object.class);
PropertyDescriptor[] pds=bi.getPropertyDescriptors();
for(PropertyDescriptor pd:pds){
String name=pd.getName();
System.out.println(name);
}
}
@Test
public void test2() throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
Person p=new Person();
BeanInfo bi=Introspector.getBeanInfo(Person.class);
PropertyDescriptor[] pds=bi.getPropertyDescriptors();
for(PropertyDescriptor pd:pds){
String name=pd.getName();
if(name.equals("name")){
Method m=pd.getWriteMethod();
m.invoke(p, "abc");
}
}
System.out.println(p.getName());
}
@Test
public void Test3() throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
Person p=new Person();
PropertyDescriptor pd=new PropertyDescriptor("name", p.getClass());
Method m=pd.getWriteMethod();
m.invoke(p,"ffff");
System.out.println(p.getName());
}
@Test
public void Test4() throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
Person p=new Person();
p.setName("abcdef");
PropertyDescriptor pd=new PropertyDescriptor("name",p.getClass());
Method m=pd.getReadMethod();
String str=(String)m.invoke(p, null);
System.out.println(str);
}
}
- 请使用beanUitls框架操作bean的属性,然后会自定义转换器
- 1.请使用sun 内省api得到某一个bean的所有属性,并操作bean的其中一个属性(给属性赋值,以及得到属性的值) 。
- 请使用sun 内省api得到某一个bean的所有属性,并操作bean的其中一个属性(给属性赋值,以及得到属性的值
- 使用内省api操作bean的属性
- 使用beanutils操作bean的属性
- 使用BeanUtils操作Bean的各种属性
- Spring自定义属性转换器
- BeanUtils操作Bean的属性
- BeanUtils操作bean的属性
- Java中使用内省操作Bean的所有属性
- JSF--转换器的使用和自定义
- ssm:关于自定义转换器的使用
- 使用内省(Introspector)操作bean属性
- 学会使用BeanUtils 来操作bean属性
- 32-35. Struts2_类型转换错误消息的显示和定制&自定义类型转换器&类型转换器与复杂属性&集合协同使用
- Spring框架bean的scope属性
- 请介绍一下Spring框架中Bean的生命周期
- 请介绍一下Spring框架中Bean的生命周期
- 史上最全的css hack(ie6-9,firefox,chrome,opera,safari)
- 史上最全的css hack(ie6-9,firefox,chrome,opera,safari)
- 请使用sun 内省api得到某一个bean的所有属性,并操作bean的其中一个属性(给属性赋值,以及得到属性的值
- 优化SQL查询:如何写出高性能SQL语句
- Core Mappings in Action-Oriented Web Framework
- 请使用beanUitls框架操作bean的属性,然后会自定义转换器
- Smack类库详细介绍
- extjs4_treestore_s2sh整合,不容易啊,折腾2天
- 笔记-2003-Chinese Word Segmentation as LMR Tagging
- 关于_main 和main ,__rt_lib_init的讨论18573103324
- Android中打印代码的调用层次
- 开源日志:Linus Torvalds’s Lessons on Software Development Management
- secureCRT登陆虚拟机ubuntu
- 为不同的语言和硬件创建资源