如果需要提交的表单内容比较多,一个JSP页面不够用,就需要多个表单依次提交,最后一起汇总给
ActionServlet处理
JSP1页面(其中hidden字段很重要,表示是第一个提交页面)
<
html
>
< head >
< title > jsp1 </ title >
</ head >
< body bgcolor ="#ffffff" >
< html:form action ="/insertAction.do" >
< html:hidden property ="page" value ="1" />
name: < html:text property ="name" />
pass: < html:text property ="password" />
< html:submit > submit
</ html:submit >
</ html:form >
</ body >
</ html >
< head >
< title > jsp1 </ title >
</ head >
< body bgcolor ="#ffffff" >
< html:form action ="/insertAction.do" >
< html:hidden property ="page" value ="1" />
name: < html:text property ="name" />
pass: < html:text property ="password" />
< html:submit > submit
</ html:submit >
</ html:form >
</ body >
</ html >
JSP2页面
<
html
>
< head >
< title > jsp2 </ title >
</ head >
< body bgcolor ="#ffffff" >
< html:form action ="/insertAction.do" >
< html:hidden property ="page" value ="2" />
city: < html:text property ="city" />
< html:submit > submit
</ html:submit >
</ html:form >
</ body >
</ html >
< head >
< title > jsp2 </ title >
</ head >
< body bgcolor ="#ffffff" >
< html:form action ="/insertAction.do" >
< html:hidden property ="page" value ="2" />
city: < html:text property ="city" />
< html:submit > submit
</ html:submit >
</ html:form >
</ body >
</ html >
为两个页面建立共同的ActionForm,注意,scope要为session
public
class
InsertForm
extends
ActionForm
...
{
private String name=null;
private String password=null;
private String city=null;
private String page=null;
public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest
httpServletRequest) ...{
ActionErrors errors=new ActionErrors();
int numpage=0;
try ...{
numpage=new Integer(page).intValue();
} catch (Exception ex) ...{
}
if(numpage==1)...{
if((name==null)||(name.length()<1))...{
errors.add("name",new ActionMessage("123"));
}
if((password==null)||(password.length()<1))...{
errors.add("password",new ActionMessage("123"));
}
}
if(numpage==2)...{
if((city==null)||(city.length()<1))...{
errors.add("city",new ActionMessage("123"));
}
}
return errors;
}
public void reset(ActionMapping actionMapping,
HttpServletRequest servletRequest) ...{
int numpage=0;
try ...{
numpage=new Integer(servletRequest.getParameter
("page")).intValue();
} catch (Exception ex) ...{
}
if(numpage==1)...{
name=null;
password=null;
}
if(numpage==2)...{
city=null;
}
}
public String getCity() ...{
return city;
}
public String getName() ...{
return name;
}
public String getPage() ...{
return page;
}
public String getPassword() ...{
return password;
}
public void setPassword(String password) ...{
this.password = password;
}
public void setPage(String page) ...{
this.page = page;
}
public void setName(String name) ...{
this.name = name;
}
public void setCity(String city) ...{
this.city = city;
}
}
private String name=null;
private String password=null;
private String city=null;
private String page=null;
public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest
httpServletRequest) ...{
ActionErrors errors=new ActionErrors();
int numpage=0;
try ...{
numpage=new Integer(page).intValue();
} catch (Exception ex) ...{
}
if(numpage==1)...{
if((name==null)||(name.length()<1))...{
errors.add("name",new ActionMessage("123"));
}
if((password==null)||(password.length()<1))...{
errors.add("password",new ActionMessage("123"));
}
}
if(numpage==2)...{
if((city==null)||(city.length()<1))...{
errors.add("city",new ActionMessage("123"));
}
}
return errors;
}
public void reset(ActionMapping actionMapping,
HttpServletRequest servletRequest) ...{
int numpage=0;
try ...{
numpage=new Integer(servletRequest.getParameter
("page")).intValue();
} catch (Exception ex) ...{
}
if(numpage==1)...{
name=null;
password=null;
}
if(numpage==2)...{
city=null;
}
}
public String getCity() ...{
return city;
}
public String getName() ...{
return name;
}
public String getPage() ...{
return page;
}
public String getPassword() ...{
return password;
}
public void setPassword(String password) ...{
this.password = password;
}
public void setPage(String page) ...{
this.page = page;
}
public void setName(String name) ...{
this.name = name;
}
public void setCity(String city) ...{
this.city = city;
}
}
以上是两个html表单都对应I"nsertForm,在创建InsertForm时有以下几点需要注意
(1)提供html表单的隐藏字段page对应的page属性
(2)在reset方法中,只把和当前正在处理的表单相关的属性恢复为默认值
(3)在validate中,仅对和当前表单相关的属性进行验证
Struts-config.xml内容
<
struts-config
>
< form-beans >
< form-bean name ="insertForm" type ="untitled2.InsertForm" />
</ form-beans >
< action-mappings >
< action input ="/jsp1.jsp" name ="insertForm"
parameter ="/jsp2.jsp" path ="/insertAction"
scope ="session"
type ="org.apache.struts.actions.ForwardAction"
validate ="true" />
< action input ="/jsp2.jsp" name ="insertForm"
path ="/insertAction2" scope ="session"
type ="untitled2.InsertAction2" validate ="true" />
</ action-mappings >
< message-resources parameter ="ApplicationResources" />
</ struts-config >
< form-beans >
< form-bean name ="insertForm" type ="untitled2.InsertForm" />
</ form-beans >
< action-mappings >
< action input ="/jsp1.jsp" name ="insertForm"
parameter ="/jsp2.jsp" path ="/insertAction"
scope ="session"
type ="org.apache.struts.actions.ForwardAction"
validate ="true" />
< action input ="/jsp2.jsp" name ="insertForm"
path ="/insertAction2" scope ="session"
type ="untitled2.InsertAction2" validate ="true" />
</ action-mappings >
< message-resources parameter ="ApplicationResources" />
</ struts-config >
JSP1页面的action为/insertAction.do
JSP2页面的action为/insertAction2.do