Struts 跨页提交表单

问题描述:有时,表单数据太多,无法在同一个页面显示,需要分页完成(如用户注册表单)。这时,既可以为每一个表单创建一个ActionForm,也可以只创建一个ActionForm,它和多个表单对应。这里讨论如何用一个ActionForm对应表单。

1.把HTML表单拆分到多个JSP页面中

这里我们把注册表单拆分为两个表单:第一个在insertContent.jsp中定义,包括name和phone字段,第二个表单在insertContent_next.jsp中定义,包括address字段。这两个表单分别对应不同的Action: “/insert1”和“/insert2”,但是这两个Action与同一个ActionForm映射。

注意在insertContent.jsp和insertContent_next.jsp中定义一个隐含字段page,它代表当前页面编号,AcitonForm将通过这个字段来识别当前正在处理的是哪个表单。insertContent.jsp中:

Html代码 复制代码
  1. <html:form action="/insert1.do">  
  2.   
  3.        <html:hidden property="page" value="1" />省略. . . . . .   
  4.   
  5. </html:form>  
  6.   
  7. insertContent_next.jsp类似:   
  8.   
  9.     <html:form action="/insert2.do">  
  10.   
  11.        <html:hidden property="page" value="2" />省略. . . . . .   
  12.   
  13.     </html:form>  



2.创建和多个HTML表单对应的ActionForm(InsertForm.java)

需要注意一下几点:

(1)提供和HTML表单的隐藏字段page对应的page属性,并生成get和set方法

(2)    在reset()方法中,只能把和当前正在处理的表单相关的属性恢复为默认值,否则,如果每次都把ActionForm的所有属性恢复为默认值,将使用户输入的上一页表单数据丢失。由于Struts框架先调用reset()方法,然后再把用户输入的表单数据组装到ActionForm中,因此在reset()方法中,不能根据page属性来判断处理的是哪个页面,而应该直接从HttpServletRequest对象中读取当前表单的page字段值:page=new Integer(request.getParameter("page")).intValue();

完整代码如下:

Java代码 复制代码
  1. public void reset(ActionMapping mapping, HttpServletRequest request) {   
  2.   
  3.        //根据“page”的值处理不同的字段   
  4.   
  5.        int numPage = 0;   
  6.   
  7.          try {   
  8.   
  9.                numPage = new Integer(request.getParameter("page")).intValue();   
  10.   
  11.          } catch (Exception e){}   
  12.   
  13.          if (numPage == 1){name = null;   
  14.   
  15.                 phone = null;}   
  16.   
  17.          if (numPage == 2){ address = null;}   
  18.   
  19.          page = null;   
  20.   
  21.     }  


(3) 在validate()方法中,仅对和当前表单相关的属性进行验证。由于Struts框架在调用validate()方法之前,已经把用户输入的表单数据组装到ActionForm中,因此在validate()方法中可以根据page属性决定正在处理哪个表单。 代码如下:   

Java代码 复制代码
  1. public ActionErrors validate(ActionMapping mapping,   
  2.   
  3.            HttpServletRequest request) {   
  4.   
  5.        ActionErrors errors = new ActionErrors();   
  6.   
  7.         int numPage = 0;   
  8.   
  9.         try{   
  10.   
  11.                numPage = new Integer(page).intValue();   
  12.   
  13.         } catch (Exception e){   
  14.   
  15.         }   
  16.   
  17.         if (numPage == 1){   
  18.   
  19.                if (((name == null) || (name.length() < 1)))   
  20.   
  21.                       errors.add("name"new ActionMessage("error.name.required"));   
  22.   
  23.                if (((phone == null) || (phone.length() < 1)))   
  24.   
  25.                       errors.add("phone"new ActionMessage("error.phone.required"));   
  26.   
  27.         }   
  28.   
  29.         if (numPage == 2) {   
  30.   
  31.                if (((address == null) || (address.length() < 1)))   
  32.   
  33.                       errors.add("address"new ActionMessage(   
  34.   
  35.                                     "error.address.required"));   
  36.   
  37.         }   
  38.   
  39.         return errors;   
  40.   
  41. }  



3.配置ActionForm和多个Action映射
当ActionForm 与多个表单对应时,应该把 ActionForm 存放在 session 中,当用户提交第一个表单时,请求由 org.apache.strutsactions.ForwardAction来处理,ForwardAction 类是 Struts 框架内置的 Action 类,他的 execute()方法负责把请求再转发给<action>元素的 parameter 属性指定的 web 组件。当用户提交第 二个表单时,请求被转发给相应的 InsertAction.
struts-config.xml 源代码

Xml代码 复制代码
  1.  <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">  
  3.   
  4. <struts-config>  
  5.   
  6. <data-sources />  
  7.   
  8. <form-beans >  
  9.   
  10.     <form-bean name="insertForm" type="com.yourcompany.struts.form.InsertForm" />  
  11.   
  12. </form-beans>  
  13.   
  14. <global-exceptions />  
  15.   
  16. <global-forwards />  
  17.   
  18. <action-mappings >  
  19.   
  20.    <action   path="/insert1"  
  21.   
  22.               parameter="/insertContent_next.jsp"  
  23.   
  24.               type="org.apache.struts.actions.ForwardAction"  
  25.   
  26.               name="insertForm"  
  27.   
  28.               scope="session"  
  29.   
  30.               input="/insertContent.jsp"  
  31.   
  32.               validate="true">  
  33.   
  34.     </action>  
  35.   
  36.     <action   path="/insert2"  
  37.   
  38.               type="com.yourcompany.struts.action.InsertAction"  
  39.   
  40.               name="insertForm"  
  41.   
  42.               scope="session"  
  43.   
  44.               input="/insertContent_next.jsp"  
  45.   
  46.               validate="true">  
  47.   
  48.      </action>  
  49.   
  50. </action-mappings>  
  51.   
  52. <message-resources parameter="com.yourcompany.struts.ApplicationResources" />  
  53.   
  54. </struts-config>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值