什么时候会用到beanutil?
解决方案 »
- 使用相对路径获取文件
- struts2上传文件时拦截器不起作用
- Tomcat错误,困扰了我好几天了
- 利用<html:select><html:options>标签显示集合的值,同时把选中的值利用<html:form>标签提交到后台.
- 如何动态实现级联菜单
- 急!!!!!!!Jsp+Bean!!!
- struts中怎么样怎么实现select中有selected的项?
- 将class文件用混淆器处理后访问的时候出现怪问题!!!
- 200分,懒得找了,谁有EJB编程指南的例子原代?
- 为什么通过散列算法后输出的长度都是一样的呀?
- webservice xifre客户端调用出错
- 使用httpinvoker必须使用SpringMVC么
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).
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
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());
}}