---
title: "Struts分步表单提交"
author: "freeglad"
date: "2008-11-06T11:14:00Z"
tags: []
slug: "struts"
draft: false
lastmod: "2008-11-06T11:14:00Z"
id: 586548
url: "https://freeglad.iteye.com/blog/586548"
---

如果需要提交的表单内容比较多,一个JSP页面不够用,就需要多个表单依次提交,最后一起汇总给  
ActionServlet处理

JSP1页面(其中hidden字段很重要,表示是第一个提交页面)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;html&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;head&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;title&gt;jsp1&lt;/title&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/head&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;bodybgcolor="#ffffff"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;html:formaction="/insertAction.do"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;html:hiddenproperty="page"value="1"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)name:&lt;html:textproperty="name"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)pass:&lt;html:textproperty="password"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;html:submit&gt;submit  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/html:submit&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/html:form&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/body&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/html&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

JSP2页面

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;html&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;head&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;title&gt;jsp2&lt;/title&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/head&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;bodybgcolor="#ffffff"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;html:formaction="/insertAction.do"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;html:hiddenproperty="page"value="2"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)city:&lt;html:textproperty="city"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;html:submit&gt;submit  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/html:submit&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/html:form&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/body&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/html&gt;

为两个页面建立共同的ActionForm,注意,scope要为session

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclassInsertFormextendsActionForm...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateStringname=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateStringpassword=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateStringcity=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateStringpage=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicActionErrorsvalidate(ActionMappingactionMapping,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)HttpServletRequest  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)httpServletRequest)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ActionErrorserrors=newActionErrors();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intnumpage=0;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)try...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)numpage=newInteger(page).intValue();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(Exceptionex)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(numpage==1)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if((name==null)||(name.length()&lt;1))...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)errors.add("name",newActionMessage("123"));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if((password==null)||(password.length()&lt;1))...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)errors.add("password",newActionMessage("123"));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(numpage==2)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if((city==null)||(city.length()&lt;1))...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)errors.add("city",newActionMessage("123"));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnerrors;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicvoidreset(ActionMappingactionMapping,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)HttpServletRequestservletRequest)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intnumpage=0;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)try...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)numpage=newInteger(servletRequest.getParameter  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)("page")).intValue();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(Exceptionex)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(numpage==1)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)name=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)password=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(numpage==2)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)city=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicStringgetCity()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returncity;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicStringgetName()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnname;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicStringgetPage()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnpage;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicStringgetPassword()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnpassword;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidsetPassword(Stringpassword)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.password=password;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidsetPage(Stringpage)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.page=page;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidsetName(Stringname)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.name=name;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidsetCity(Stringcity)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.city=city;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

以上是两个html表单都对应I"nsertForm,在创建InsertForm时有以下几点需要注意

（１）提供html表单的隐藏字段page对应的page属性

（２）在reset方法中，只把和当前正在处理的表单相关的属性恢复为默认值

（３）在validate中，仅对和当前表单相关的属性进行验证

Ｓｔｒｕｔｓ－ｃｏｎｆｉｇ．ｘｍｌ内容

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;struts-config&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;form-beans&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;form-beanname="insertForm"type="untitled2.InsertForm"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/form-beans&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;action-mappings&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;actioninput="/jsp1.jsp"name="insertForm"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)　　　　　　　parameter="/jsp2.jsp"path="/insertAction"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)　　　　　　　scope="session"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)　　　　　　　type="org.apache.struts.actions.ForwardAction"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)　　　　　　　validate="true"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;actioninput="/jsp2.jsp"name="insertForm"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)　　　　　　　path="/insertAction2"scope="session"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)　　　　　　　type="untitled2.InsertAction2"validate="true"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/action-mappings&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;message-resourcesparameter="ApplicationResources"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/struts-config&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

ＪＳＰ１页面的action为/insertAction.do

ＪＳＰ２页面的action为/insertAction２.do