`

使用动态ActionForm(转)

阅读更多
在Struts框架中,ActionForm对象用来包装HTML表单数据,并能动态返回用于显示给用户的数据,自定义的ActionForm必需符合 JavaBean规范,并继承Struts的ActionForm类,同时用户可以选择的覆盖两个方法:reset()和validate().
    ActionForm的唯一缺点是对于大型的Struts应用,必需以编程的方式创建大量的ActionForm类,如果HTML表单字段发生变化,就必 须修改并编译相关的ActionForm类。Struts1.1对此进行了改进,引入了动态ActionForm类的概念,Struts框架的 DynaActionForm类及其子类实现了动态ActionForm,该类是ActionForm类的子类。
1、配置动态ActionForm
    动态ActionForm支持在Struts配置文件中完成ActionForm的全部配置,没有必要编写额外的程序创建具体的ActionForm类, 配置动态ActionForm的方法为:在Struts配置文件中配置一个<form-bean>元素,将type属性设置为 DynaActionForm或它的某个子类的全名,例如:
     <form-beans>
        <form-bean
            name="loginForm"
            type="org.apache.struts.action.DynaActionForm">
           <form-property
              name = "email"
              type="java.lang.String"/>
            <form-property
              name = "password"
              type="java.lang.String"/>
             <form-property
              initial="false"      //设置初始值
              name = "rememberMe"
              type="java.lang.Boolean"/>
        </form-bean>
    </form-beans>
   <form-bean>的<form-property>子元素用来设置动态ActionForm的属性,<form- property>元素的name属性指定属性名,type指定属性类型,可以把动态的ActionForm的属性设置为以下Java类型:
• java.lang.BigDecimal
• java.lang.BigInteger
• java.lang.Boolean
• java.lang.Byte
• java.lang.Character
• java.lang.Class
• java.lang.Double
• java.lang.Float
• java.lang.Integer
• java.lang.Long
• java.lang.Short
• java.lang.String
• java.sql.Date
• java.sql.Time
• java.sql.Timestamp
    如果表单的字段值为Java基本类型,在配置时应该用相应的包装类型来代替,比如:int类型的包装类型为Integer:
      <form-property
                initial="0"
                name="age"
                type="java.lang.Integer"/>
2、动态ActionForm的reset()方法
     DynaActionForm基类提供了initialize()方法,它把表单的所有属性恢复为默认值。表单属性默认值由<form- bean>的<form-property>子元素的initial属性来决定。如果没设置initial属性,则表单属性的默认值由 其java类型来自动决定。
   DynaActionForm基类的initialize()方法如下:
     public void initialize(ActionMapping mapping){
                   String name = mapping.getName();
                   if(name==null){
                         return;
                   }
                  FormBeanConfig config =
                          mapping.getModuleConfig().findFormBeanConfig(name);
                  if(config==null){
                       return;
                  }
                  FormPropertyConfig props[] = config.findFormPropertyConfigs();
                 for (int i = 0;i<props.length;i++){
                          set(props[i].getName(),props[i].initial());
                  }
        }
   DynaActionForm基类的reset()方法不执行任何操作,代码如下:
     public void reset(ActionMapping mapping,HttpServletRequest request){
    }
   如果希望Struts框架在每次把表单数据组装到动态ActionForm中之前,先把所有的属性恢复为默认值,可以定义一个扩展DynaActionForm类的子类,然后覆盖其reset()方法,在reset方法中只要调用initialize()方法即可。
3、访问动态ActionForm
   Action类和JSP都可以访问动态ActionForm,使用方法与标准ActionForm大致相同,只有一点小差别,如果使用标准的ActionForm对象,在标准的ActionForm中针对每个属性都提供了get/set方法,来读取或设置属性。
   而DynaActionForm把所有的属性保存在一个Map类对象中,并提供了下面的用于访问所有属性的通用方法:
     public Object get(String name);
     public void set(String name,Object value);
    get(String name)方法根据指定的属性名返回属性值;set(String name,Object value)方法用于为给定的属性赋值。例如,如果访问DynaActionForm类中的email属性,可以采用:
    //get email
    String email = (String)form.get("email");
   // set email
   form.set("email",example@example.com);
4、动态ActionForm的表单验证
   DynaActionForm基类的validate()方法没有提供任何默认的验证行为,可以定义扩展的DynaActionForm的子类,然后覆盖 validate()方法,但是以编程的方式来验证动态ActionForm违背了Struts框架提供动态ActionForm的初衷,即以配置来代替 编程,可以采用另一种验证机制,即Validator框架来完成验证,该框架允许采用特定的配置文件来为动态的ActionForm配置验证规则。
5、在应用中使用动态ActionForm
   在应用中定义了名为"itemDetailForm"的动态ActionForm,它包含了一个ItemDetailView类型的属性view,
<form-bean
       name="itemDetailForm"
      dynamic="true"
       type="org.apache.struts.action.DynaActionForm">
       <form-property name="view" type="netstore.catalog.view.ItemDetailView"/>       
   </form-bean>
在JSP页面上,如果选择了某个商品,该请求将转发给GetItemDetailAction来处理,配置如下:
<action
     path="/viewitemdetail"
     name="itemDetailForm"
     input="/index.jsp"
     type="netstore.catalog.GetItemDetailAction"  
     scope="request"     
     validate="false">
     <exception
         key="global.error.invalidlogin"
         path="/index.jsp"
         scope="request"
     type="netstore.framework.exceptions.DatastoreException"/>
   
     <forward name="Success" path="/catalog/itemdetail.jsp"/>
</action>
GetItemDetailAction根据用户选择商品的ID调用模型的业务方法,并检索出该商品的详细信息,把它存放在ItemDetailView 对象中,再把ItemDetailView对象赋值给动态itemDetailForm的view属性,GetItemDetailAction的 execute()方法的代码如下:
     public ActionForward execute( ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response )
    throws Exception {
     // Get the primary key of the item from the request
     String itemId = request.getParameter( IConstants.ID_KEY );
     // Call the netstore service and ask it for an ItemView for the item
     INetstoreService serviceImpl = getNetstoreService();
     ItemDetailView itemDetailView = serviceImpl.getItemDetailView( itemId );
     // Set the returned ItemView into the Dynamic Action Form
     // The parameter name 'view' is what is defined in the struts-config
     ((DynaActionForm)form).set("view", itemDetailView);
     //String userName = ((DynaActionForm)form).get( "username" );
     //((DynaActionForm)form).set( "password", password );

     // Return the ActionForward that is defined for the success condition
     return mapping.findForward( IConstants.SUCCESS_KEY );
   }
GetItemDetailAction最后把请求转发给itemdetail.jsp,它通过<bean:write>标签把动态itemDetailForm的view属性包含的数据输出到网页上,
<bean:write name="itemDetailForm" property="view.name"/><BR>
</B></font><font   size=2>
   <!-- PRODUCT MODEL --><bean:message key="itemdetail.model"/><bean:write name="itemDetailForm" property="view.modelNumber"/><BR><BR><!-- /PRODUCT MODEL --></font>
   <font   size=2>
<!-- PRODUCT DESCRIPTION -->
<bean:write name="itemDetailForm" property="view.description"/>
分享到:
评论
1 楼 yulongxiang 2010-11-09  
非常感谢!!!!!!!

相关推荐

Global site tag (gtag.js) - Google Analytics