mport java.lang.reflect.Method;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class BeanBind {
public static Log log = LogFactory.getLog(BeanBind.class);
// choose the appropriate method to invoke and set the class properity
public static Method select(Method[] method, String name, Class clz) {
Method methods=null;
for (int i = 0; i < method.length; i++) {
int a = method[i].getName().length();
if (method[i].getName().substring(3, a).equalsIgnoreCase(name)
&& (method[i].getName().startsWith("set"))) {
log.info("get the appropriate method: "+clz.getClass()+"."+method[i].getName());
return method[i];
}
}
System.out.println("check");
return null;
}
//get the data from request and bind into Class clz
public static Object bind(Class clz,HttpServletRequest request) throws InstantiationException,
IllegalAccessException {
java.lang.reflect.Field[] field = clz.getDeclaredFields();
Method[] method = clz.getDeclaredMethods();
Object obj = clz.newInstance();
System.out.println("field.length: "+field.length);
for (int i = 0; i < field.length; i++) {
String parser = null;
Method meod = null;
String type = field[i].getType().getSimpleName();
String str = field[i].getName();
parser=request.getParameter(str);
meod = select(method, str, clz);
log.info("method: "+meod.getName()+ " bind the properity: "+str+" value : "+parser);
if (type.equalsIgnoreCase("String")) {
try {
Object[] arg = { parser };
System.out.println("String");
meod.invoke(obj, arg);
} catch (Exception e) {
log.error("String value: "+parser+ "invoke error: "+clz.getName());
}
} else if (type.equalsIgnoreCase("Integer")) {
try {
Object[] arg = { Integer.valueOf(parser) };
meod.invoke(obj, arg);
} catch (Exception e) {
log.error("Integer value: "+parser+ "invoke error: "+clz.getName());
}
} else if (type.equalsIgnoreCase("Double")) {
try {
Object[] arg = { Double.valueOf(parser) };
meod.invoke(obj, arg);
} catch (Exception e) {
log.error("Double value: "+parser+ "invoke error: "+clz.getName());
}
} else if (type.equalsIgnoreCase("Float")) {
try {
Object[] arg = { Float.valueOf(parser) };
meod.invoke(obj, arg);
} catch (Exception e) {
log.error("Float value: "+parser+ "invoke error: "+clz.getName());
}
} else if (type.equalsIgnoreCase("date")) {
try {
Date date = new Date(parser);
Object[] arg ={ date };
meod.invoke(obj, arg);
} catch (Exception e) {
log.error("Date value: "+parser+ "invoke error: "+clz.getName());
}
}
System.out.println("ahaha");
}
return obj;
}
}
具体的调用方法如下.相应的值是从HttpRequest (Form表单)获取 关于验证方法可以在JavaBean中自己定义一个validator方法.然后动态调用
User user=new User();
Class clzs=user.getClass();
user=(User) BeanBind.bind(clzs, request);
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class BeanBind {
public static Log log = LogFactory.getLog(BeanBind.class);
// choose the appropriate method to invoke and set the class properity
public static Method select(Method[] method, String name, Class clz) {
Method methods=null;
for (int i = 0; i < method.length; i++) {
int a = method[i].getName().length();
if (method[i].getName().substring(3, a).equalsIgnoreCase(name)
&& (method[i].getName().startsWith("set"))) {
log.info("get the appropriate method: "+clz.getClass()+"."+method[i].getName());
return method[i];
}
}
System.out.println("check");
return null;
}
//get the data from request and bind into Class clz
public static Object bind(Class clz,HttpServletRequest request) throws InstantiationException,
IllegalAccessException {
java.lang.reflect.Field[] field = clz.getDeclaredFields();
Method[] method = clz.getDeclaredMethods();
Object obj = clz.newInstance();
System.out.println("field.length: "+field.length);
for (int i = 0; i < field.length; i++) {
String parser = null;
Method meod = null;
String type = field[i].getType().getSimpleName();
String str = field[i].getName();
parser=request.getParameter(str);
meod = select(method, str, clz);
log.info("method: "+meod.getName()+ " bind the properity: "+str+" value : "+parser);
if (type.equalsIgnoreCase("String")) {
try {
Object[] arg = { parser };
System.out.println("String");
meod.invoke(obj, arg);
} catch (Exception e) {
log.error("String value: "+parser+ "invoke error: "+clz.getName());
}
} else if (type.equalsIgnoreCase("Integer")) {
try {
Object[] arg = { Integer.valueOf(parser) };
meod.invoke(obj, arg);
} catch (Exception e) {
log.error("Integer value: "+parser+ "invoke error: "+clz.getName());
}
} else if (type.equalsIgnoreCase("Double")) {
try {
Object[] arg = { Double.valueOf(parser) };
meod.invoke(obj, arg);
} catch (Exception e) {
log.error("Double value: "+parser+ "invoke error: "+clz.getName());
}
} else if (type.equalsIgnoreCase("Float")) {
try {
Object[] arg = { Float.valueOf(parser) };
meod.invoke(obj, arg);
} catch (Exception e) {
log.error("Float value: "+parser+ "invoke error: "+clz.getName());
}
} else if (type.equalsIgnoreCase("date")) {
try {
Date date = new Date(parser);
Object[] arg ={ date };
meod.invoke(obj, arg);
} catch (Exception e) {
log.error("Date value: "+parser+ "invoke error: "+clz.getName());
}
}
System.out.println("ahaha");
}
return obj;
}
}
具体的调用方法如下.相应的值是从HttpRequest (Form表单)获取 关于验证方法可以在JavaBean中自己定义一个validator方法.然后动态调用
User user=new User();
Class clzs=user.getClass();
user=(User) BeanBind.bind(clzs, request);