您的位置:首页 > 运维架构

BeanUtils MethodUtils PropertyUtils 的使用

2016-09-29 00:00 204 查看
(1) action和form

package com.action;

public class LoginAction {

private String userName;
private String passWord;
private String sex;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}

public String sayHello(String name){
System.out.println("hello 你好 "+name);
return name;
}

public LoginAction() {
}
public LoginAction(String userName, String passWord, String sex) {
this.userName = userName;
this.passWord = passWord;
this.sex = sex;
}

}

package com.form;

public class LoginForm {

private String userName;
private String passWord;
private String sex;
private String code;

public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}

public void getActionName(){

System.out.println("LoginAction");
}
}


(2) 其余的类

package com.bean;

import java.util.List;

public class Address {
private String email;
private List<String> telephone;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public List<String> getTelephone() {
return telephone;
}
public void setTelephone(List<String> telephone) {
this.telephone = telephone;
}
}


package com.bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Person {
private String username;
private int age;
private float stature;//身高
private boolean sex;//性别
@SuppressWarnings("unchecked")
private List list = new ArrayList();
private String[] friendsNames;
private Map<String, String> maps = new HashMap<String, String>();
private Address address;

public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public float getStature() {
return stature;
}
public void setStature(float stature) {
this.stature = stature;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
@SuppressWarnings("unchecked")
public List getList() {
return list;
}
@SuppressWarnings("unchecked")
public void setList(List list) {
this.list = list;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Map<String, String> getMaps() {
return maps;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public String[] getFriendsNames() {
return friendsNames;
}
public void setFriendsNames(String[] friendsNames) {
this.friendsNames = friendsNames;
}
}

package com.bean;

public class Teacher {
private String id;

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}


(3) 测试

a.BeanUtils

package com.test;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;

import com.action.LoginAction;
import com.form.LoginForm;

public class BeanUtilsTest {

public static void main(String[] args) throws IllegalAccessException, InvocationTargetException {

LoginForm loginForm = new LoginForm();
loginForm.setUserName("admin");
loginForm.setPassWord("1234");
loginForm.setSex("男");

LoginAction loginAction = new LoginAction();

BeanUtils.copyProperties(loginAction, loginForm);

System.out.println(loginAction.getUserName());

}
}


b.MethodUtils

package com.test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.MethodUtils;
import org.apache.commons.beanutils.PropertyUtils;

import com.action.LoginAction;

public class MethodUtilsTest {
public static void main(String[] args) throws Exception{
MethodUtilsTest methodUtilsTest  = new MethodUtilsTest();

Constructor constructor = methodUtilsTest.getClass(LoginAction.class).getConstructor(new Class[]{String.class,String.class,String.class});
Object action = constructor.newInstance(new String[]{"admin","1234","N"});
LoginAction loginAction = (LoginAction) action;

PropertyUtils.setProperty(loginAction, "sex", "G");
System.out.println(loginAction.getSex());

Object name = MethodUtils.invokeMethod(loginAction, "sayHello", "wxl");
System.out.println(name.toString());
}

public Class getClass(Class c){
return c;
}
}


c.PropertyUtils

package com.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;

import com.bean.Address;
import com.bean.Person;

public class PropertyUtilsTest {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Person person = new Person();
try {
//simple property
PropertyUtils.setProperty(person, "username", "李四");
PropertyUtils.setSimpleProperty(person, "age", 22);
PropertyUtils.setSimpleProperty(person, "stature", 173.5f);
PropertyUtils.setSimpleProperty(person, "sex", new Boolean(false));

//index property
//List
List list = new ArrayList();
list.add("list value 0");
list.add("list value 1");
String listValue2 = "new list value 1";
PropertyUtils.setSimpleProperty(person, "list", list);
//将list设置到person之后,可以对里面的值进行修改
PropertyUtils.setIndexedProperty(person, "list[1]", listValue2);

//数组
String[] str = {"张三", "王五", "赵钱"};
person.setFriendsNames(str);
PropertyUtils.setIndexedProperty(person, "friendsNames[2]", "new赵钱");

//Map
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "vlaue1");
map.put("key2", "vlaue2");
map.put("key3", "vlaue3");
person.setMaps(map);
PropertyUtils.setMappedProperty(person, "maps", "key1", "new value1");
PropertyUtils.setMappedProperty(person, "maps(key2)", "maps(key2) value");

//nest property
Address address = new Address();
address.setEmail("jhlishero@163.com");
List<String> telephoneList = new ArrayList<String>();
telephoneList.add("12345678911");
telephoneList.add("92345678911");
address.setTelephone(telephoneList);
person.setAddress(address);
PropertyUtils.setNestedProperty(person, "address.telephone[1]", "nest 11111111");
PropertyUtils.setNestedProperty(person, "address.email", "new_jhlishero@163.com");

System.out.println(PropertyUtils.getSimpleProperty(person, "username"));
System.out.println(PropertyUtils.getSimpleProperty(person, "age"));
System.out.println(PropertyUtils.getSimpleProperty(person, "stature"));
System.out.println(PropertyUtils.getSimpleProperty(person, "sex"));
System.out.println(PropertyUtils.getSimpleProperty(person, "list"));
//list
System.err.println(PropertyUtils.getIndexedProperty(person, "list[0]"));
System.err.println(PropertyUtils.getIndexedProperty(person, "list", 1));
//数组
System.out.println(PropertyUtils.getIndexedProperty(person, "friendsNames[0]"));
System.out.println(PropertyUtils.getIndexedProperty(person, "friendsNames", 1));
System.out.println(PropertyUtils.getIndexedProperty(person, "friendsNames[2]"));

//Map
System.err.println(PropertyUtils.getMappedProperty(person, "maps(key1)"));
System.err.println(PropertyUtils.getMappedProperty(person, "maps", "key2"));
System.err.println(PropertyUtils.getMappedProperty(person, "maps(key3)"));

//nest--嵌套输出
System.out.println(PropertyUtils.getNestedProperty(person, "address.email"));
System.out.println(PropertyUtils.getNestedProperty(person, "address.telephone[0]"));
System.out.println(PropertyUtils.getNestedProperty(person, "address.telephone[1]"));

//也可以使用如下方法获取值
System.out.println(PropertyUtils.getProperty(person, "address.telephone[1]"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
(4) 项目清单



(5) jar包下载

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: