Java反射之模拟JavaBean接收表单参数


依旧用代码说话。。

这是一个Java Project

  1. package com.jadyer.reflection;   
  2.   
  3. import java.io.FileReader;   
  4. import java.lang.reflect.Method;   
  5. import java.util.Enumeration;   
  6. import java.util.Hashtable;   
  7. import java.util.Properties;   
  8.   
  9. import com.jadyer.model.Student;   
  10.   
  11. /**  
  12.  * 模拟JavaBean接收表单参数  
  13.  */  
  14. public class ImitateJavaBean {   
  15.     private static Hashtable<String, Student> mySession; //模拟Web编程中的session对象   
  16.        
  17.     static{   
  18.         mySession = new Hashtable<String, Student>();   
  19.         mySession.put("stu"new Student());   
  20.     }   
  21.        
  22.     public static void main(String[] args) throws Exception{   
  23.         Properties properties = new Properties();   
  24.         FileReader fileReader = new FileReader("props.txt");   
  25.         properties.load(fileReader); //导入指定文件中所有属性信息   
  26.         fileReader.close(); //关闭打开的文件   
  27.            
  28.         Enumeration<?> propertiesNames = properties.propertyNames(); //获取所有的属性名   
  29.         while(propertiesNames.hasMoreElements()){   
  30.             String name = (String)propertiesNames.nextElement();   
  31.             String value = properties.getProperty(name);   
  32.             ImitateJavaBean.invokeSetter("stu", name, value); //执行setter方法,为属性赋值   
  33.         }   
  34.            
  35.         System.out.println(ImitateJavaBean.mySession.get("stu")); //输出stu标记的元素的值,即student对象   
  36.     }   
  37.        
  38.     /**  
  39.      * 调用指定属性的setter方法  
  40.      * @param beanName-------mySession中的key,通过它可以获取到对应的JavaBean对象  
  41.      * @param propertyName---外部资源文件中的name,通过它构造setter方法  
  42.      * @param propertyValue--外部资源文件中的value,通过它指定构造的setter的参数值  
  43.      */  
  44.     public static void invokeSetter(String beanName, String propertyName, String propertyValue) throws Exception{   
  45.         Object obj = mySession.get(beanName);   
  46.         Class<?> clazz = obj.getClass();   
  47.            
  48.         //动态构造一个setter方法,比如name --> setName   
  49.         //propertyName.substring(0,1)可以取出属性名的第一个字母   
  50.         //propertyName.substring(1)可以从属性名的第2个字母开始取出所有其它的字母   
  51.         String methodName = "set" + propertyName.substring(0,1).toUpperCase() + propertyName.substring(1);   
  52.            
  53.         //Method method = clazz.getMethod(methodName, java.lang.String.class) //也可以这样写   
  54.         Method method = clazz.getMethod(methodName, new Class[]{java.lang.String.class});   
  55.            
  56.         method.invoke(obj, propertyValue);//将propertyValue作为该方法的参数值,然后调用该方法   
  57.     }    
  58. }  

下面是用到的Student类

 

  1. package com.jadyer.model;   
  2.   
  3. public class Student {   
  4.     private String name;   
  5.     private String password;   
  6.        
  7.     public void setName(String name) {   
  8.         this.name = name;   
  9.     }   
  10.     public void setPassword(String password) {   
  11.         this.password = password;   
  12.     }   
  13.        
  14.     @Override  
  15.     public String toString() {   
  16.         return "Name:" + name + "\tPassword:" + password;   
  17.     }      
  18. }  

最后是用到的位于Java Project根目录中的属性文件props.txt

  1. name     = Admin   
  2. password = Jadyer  

相关内容