动态赋值弱类型值对象--Dynamic Create Value Object

来源:互联网 发布:威客网络兼职怎么赚钱 编辑:程序博客网 时间:2024/05/16 04:32
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
from web  to Value Object, Dynamic Create your Value Object  :use weake-type Value Object
摘要:在j2ee应用中,采用jsp+bean+servlet开发,采取前端校验,在
不涉及EJB的情况下,我喜欢使用弱类型值对象,本文描述了如何动态赋
值弱类型值对象。有关弱类型值对象请参考<J2EE模式>。
为什么要动态赋值弱类型值对象?
     J2EE可开发中你可能需要将大量从客户端截获的数据赋值你的bean
中,每一次都要重复大量的赋值语句,是不是感觉到很烦,采用一种合适
的策略来消除这种重复的工作,是改进你生产效率的途径。
    
如何动态赋值弱类型值对象。
    
     解决这个问题你需要确定使用指定的命名模式。页面的元素  <input
  type="text" name="p_Name>,BEAN 属性 setP_name() ,getP_name();在
使用指定的命名模式后,你就可以使用简单的代码实现动态赋值了。
    
 代码描述如下:    
    
     弱类型值对象    
     class Author{
    
      Properties  attr = new  Properties();
     
      .....................................
       public void setP_name(String name){
          attr.putProperties("NAME",name);
       }
      
      
       public String getP_name(){
        return attr.getProperties("NAME");
       }
      
       .........................
     }

      动态赋值弱类型值对象
     
      Author author = new Author();
     
      Method[] methods = Class.forName("Author").getMethods();
     
     
      String[]  names =(String[]) request.getParameterNames();
      String Value = null;
      for(int i=0;i<names.length;i++){
         Value = request.getParameter(names[i]);
         
           for(int j=0;i<methods.length;j++){    
         bname =methods[j].getName();
         if(bname.startsWith("set")){
            bname =bname.substring(3,bname.length());
          
           if(bname.equalsIgnoreCase(names(i)){
                 methods[j].invoke(author,new Object[]{Value});          
            }
             }
             }   
        }
      
    
正如你所看到的,实现动态赋值是很简单的。我将在下一篇文章from entity 
baen  to Value Object Dynamic Create your Value Object  :use  Value
Object中讲讲如何实现从ejb到标准vo的动态赋值,可能这更能使你感兴趣。

欢迎大家来讨论。
 
 
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击