分页表单的提交

来源:互联网 发布:软件质量咨询招标 编辑:程序博客网 时间:2024/05/21 11:23

问题描述:有时,表单数据太多,无法在同一个页面显示,需要分页完成(如用户注册表单)。这时,既可以为每一个表单创建一个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:form action="/insert1.do">

       <html:hidden property="page" value="1" />省略. . . . . .

</html:form>

insertContent_next.jsp类似:

    <html:form action="/insert2.do">

       <html:hidden property="page" value="2" />省略. . . . . .

    </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();

完整代码如下:

    public void reset(ActionMapping mapping, HttpServletRequest request) {

       //根据“page”的值处理不同的字段

       int numPage = 0;

         try {

               numPage = new Integer(request.getParameter("page")).intValue();

         } catch (Exception e){}

         if (numPage == 1){name = null;

                phone = null;}

         if (numPage == 2){ address = null;}

         page = null;

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

public ActionErrors validate(ActionMapping mapping,

           HttpServletRequest request) {

       ActionErrors errors = new ActionErrors();

        int numPage = 0;

        try{

                numPage = new Integer(page).intValue();

        } catch (Exception e){

        }

        if (numPage == 1){

               if (((name == null) || (name.length() < 1)))

                      errors.add("name", new ActionMessage("error.name.required"));

               if (((phone == null) || (phone.length() < 1)))

                      errors.add("phone", new ActionMessage("error.phone.required"));

        }

        if (numPage == 2) {

               if (((address == null) || (address.length() < 1)))

                       errors.add("address", new ActionMessage(

                                    "error.address.required"));

        }

        return errors;

3.配置ActionForm和多个Action映射

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

struts-config.xml 源代码如下:

        <?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>

<data-sources />

<form-beans >

    <form-bean name="insertForm" type="com.yourcompany.struts.form.InsertForm" />

</form-beans>

<global-exceptions />

<global-forwards />

<action-mappings >

   <action   path="/insert1"

              parameter="/insertContent_next.jsp"

              type="org.apache.struts.actions.ForwardAction"

              name="insertForm"

              scope="session"

              input="/insertContent.jsp"

              validate="true">

    </action>

    <action   path="/insert2"

              type="com.yourcompany.struts.action.InsertAction"

              name="insertForm"

              scope="session"

              input="/insertContent_next.jsp"

              validate="true">

     </action>

</action-mappings>

<message-resources parameter="com.yourcompany.struts.ApplicationResources" />

</struts-config>