java 运用反射,模拟类似Struts2的参数注入
在Struts2中,当你在页面中运用form 表单向后台传值是,可以采取以下形式:
<form action="login.do">
<input name="user.id" type="text" />
<input name="user.card.loacl.china" type="text" />
</form>
以上 通过 user.id / user.card.loacl.china 直接将表单页面中的值,注入到action中的对象中 (User),创建一个类模拟此功能。
- 创建对象
package com.ziv.bean;
import java.util.Arrays;
public class User {
private Integer id;
private String name;
private String[] hibbit;
private Integer[] op;
private Card card = new Card();
public void setOp(Integer[] op) {
this.op = op;
}
public Integer[] getOp() {
return op;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String[] getHibbit() {
return hibbit;
}
public void setHibbit(String[] hibbit) {
this.hibbit = hibbit;
}
public Card getCard() {
return card;
}
public void setCard(Card card) {
this.card = card;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", hibbit="
+ Arrays.toString(hibbit) + ", op=" + Arrays.toString(op)
+ ", card=" + card + "]";
}
}
package com.ziv.bean;
import java.util.Arrays;
import java.util.Date;
/*创建card 对象*/
public class Card {
private Integer id;
private String[] num;
private Date brith;
private Loacl loacl = new Loacl();
public void setNum(String[] num) {
this.num = num;
}
public String[] getNum() {
return num;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Date getBrith() {
return brith;
}
public void setBrith(Date brith) {
this.brith = brith;
}
public Loacl getLoacl() {
return loacl;
}
public void setLoacl(Loacl loacl) {
this.loacl = loacl;
}
@Override
public String toString() {
return "Card [id=" + id + ", num=" + Arrays.toString(num) + ", brith="
+ brith + ", loacl=" + loacl + "]";
}
}kage com.ziv.bean;
import java.util.Arrays;
public class Loacl {
private Integer id;
private String[] china;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String[] getChina() {
return china;
}
public void setChina(String[] china) {
this.china = china;
}
@Override
public String toString() {
return "Loacl [id=" + id + ", china=" + Arrays.toString(china) + "]";
}
}
2.编写参数注入对象
package self.ziv.servlet.refect.utils;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import com.ziv.bean.User;
/**
*
* @author ziv
* 封装自动注入参数的变懒
*/
public class ParameterInjection {
/* 源对象对应action中要注入的实体类对象 */
private Object obj;
/* 对象的构建方法 */
private Field field;
/* 要注入的对象属性 */
private String[] parms;
/* 属性的值 */
private Object valueParms;
ParameterInjection(){}
ParameterInjection(Object obj,String parm, Object valueParms){
this.obj = obj;
this.parms = parm.split("\\.");
this.valueParms = valueParms;
handleString();
}
/**
* 创建参数对象
*/
private void handleString(){
try {
for (int i = 1; i < parms.length; i++) {
if(parms.length - 1 == i){
this.field = obj.getClass().getDeclaredField(this.parms[this.parms.length - 1]);
this.field.setAccessible(true);
this.field.set(this.obj, valueParms);
}else{
Method getMet = this.obj.getClass().getMethod("get" + initcap(this.parms[i]));
this.obj = getMet.invoke(this.obj);
}
}
} catch (Exception e) {
System.out.println("ZIV -ERROR: Unknown Source -> maybe **" + this.parms[this.parms.length - 1] + "** **get/set**");
e.printStackTrace();
}
}
/**
* 參數注入
*/
public void startHandleString(){
handleString();
}
/**
* 将一个字符串的首字母大写
* @param str
* @return
*/
private String initcap(String str){
return str.toUpperCase().substring(0,1) + str.substring(1,str.length());
}
/**
* 設置 屬性
* @param parms 屬性的值
*/
public void setParm(String parm) {
this.parms = parm.split("\\.");
}
/**
* 設置 屬性的值
* @param parms 屬性的值
*/
public void setValueParms(Object valueParms) {
this.valueParms = valueParms;
}
/**
* 要注入值的對象
* @param obj
*/
public void setObj(Object obj) {
this.obj = obj;
}
// 測試
public static void main(String[] args) {
String parms = "user.card.loacl.china";
User user = new User();
ParameterInjection parameterInjection = new ParameterInjection();
parameterInjection.setObj(user);
parameterInjection.setParm(parms);
parameterInjection.setValueParms(new String[]{"sdf","揚州"});
parameterInjection.startHandleString();
System.out.println(user.getCard().getLoacl().toString());
}
}