什么时候会用到beanutil?

pleasanton 2013-07-25 11:35:47
什么时候会用到beanutil?
...全文
171 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
fedori 2013-07-26
  • 打赏
  • 举报
回复
commons-beanutils-1.7.0.jar
pleasanton 2013-07-26
  • 打赏
  • 举报
回复
where is BeanMap?
fedori 2013-07-26
  • 打赏
  • 举报
回复
JavaBean之间如果属性名称相同,用BeanUtils传递值很方便。
public class BeanSource {
	private String name;
	private int age;
	private String sex;

	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 getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

}
public class BeanDest {
	private String name;
	private int age;
	private String sex;
	private String education;
	private String experience;

	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 getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getEducation() {
		return education;
	}

	public void setEducation(String education) {
		this.education = education;
	}

	public String getExperience() {
		return experience;
	}

	public void setExperience(String experience) {
		this.experience = experience;
	}

}
import org.apache.commons.beanutils.BeanMap;

public class TestII {

	public static void main(String[] args) {

		BeanDest dest = new BeanDest();
		BeanSource source = new BeanSource();
		source.setAge(24);
		source.setName("tomcat");
		source.setSex("female");

		BeanMap beanMap = new BeanMap(dest);
		beanMap.putAllWriteable(new BeanMap(source));
		System.out.println(dest.getAge() + ":" + dest.getName() + ":" + dest.getSex());
	}

}
pleasanton 2013-07-25
  • 打赏
  • 举报
回复
BeanUtils主要提供了对于JavaBean进行各种操作。它里面提供的方法都是静态的,直接通过类名.方法名就使用。它具有以下功能: 1、 将Map转成Bean对象,它可以为通过Map给对象实例。 @Test public void testMap2Bean() throws IllegalAccessException, InvocationTargetException{ Map<String,String> map = new HashMap<String,String>(); map.put("name","wsh"); map.put("address", "河南郑州"); Person person = new Person(); BeanUtils.populate(person, map); System.out.println(person.getAddress()+ " " + person.getName()); } 2、 对象的克隆。 @Test public void testClone() throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException{ Person person = new Person(); person.setAddress("湖南湘潭"); person.setName("wsh"); Person person2 = (Person) BeanUtils.cloneBean(person); System.out.println(person2.getAddress()+ " " + person2.getName()); System.out.println(person.equals(person2)); } 3、 属性动态的getter和setter @Test public void testGetSet() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{ Person person = new Person(); person.setAddress("湖南湘潭"); person.setName("wsh"); String name = BeanUtils.getProperty(person,"name"); System.out.println(name); BeanUtils.setProperty(person, "address", "火星"); System.out.println(person.getAddress()); } 4、 动态读取Collection,Map
pleasanton 2013-07-25
  • 打赏
  • 举报
回复
操作Bean的工具,其实现原理就是采用了反射,在servlet处理请求参数,处理ResultSet数据中经常用到,在不用MVC框架下可以很大的优化代码 用MVC框架下,还需要用beanutil吗?
pleasanton 2013-07-25
  • 打赏
  • 举报
回复
However, there are some occasions where dynamic access to Java object properties (without compiled-in knowledge of the property getter and setter methods to be called) is needed. Example use cases include: Building scripting languages that interact with the Java object model (such as the Bean Scripting Framework). Building template language processors for web presentation and similar uses (such as JSP or Velocity). Building custom tag libraries for JSP and XSP environments (such as Jakarta Taglibs, Struts, Cocoon). Consuming XML-based configuration resources (such as Ant build scripts, web application deployment descriptors, Tomcat's server.xml file).
S117 2013-07-25
  • 打赏
  • 举报
回复
是Common BeanUtils吗?如果是的话,就是在你 用javaBean的时候呀!

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧