ITeye博客

Struts分步表单提交

freeglad · 2008-11-06 11:14 · struts · Markdown

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

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

<html>
<head>
<title>jsp1</title>
</head>
<bodybgcolor="#ffffff">
<html:formaction="/insertAction.do">
<html:hiddenproperty="page"value="1"/>
name:<html:textproperty="name"/>
pass:<html:textproperty="password"/>
<html:submit>submit
</html:submit>
</html:form>
</body>
</html>

JSP2页面

<html>
<head>
<title>jsp2</title>
</head>
<bodybgcolor="#ffffff">
<html:formaction="/insertAction.do">
<html:hiddenproperty="page"value="2"/>
city:<html:textproperty="city"/>
<html:submit>submit
</html:submit>
</html:form>
</body>
</html>

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

publicclassInsertFormextendsActionForm...{
privateStringname=null;
privateStringpassword=null;
privateStringcity=null;
privateStringpage=null;
publicActionErrorsvalidate(ActionMappingactionMapping,
HttpServletRequest
httpServletRequest)...{

ActionErrorserrors=newActionErrors();
intnumpage=0;
try...{
numpage=newInteger(page).intValue();
}catch(Exceptionex)...{
}
if(numpage==1)...{
if((name==null)||(name.length()<1))...{
errors.add("name",newActionMessage("123"));
}
if((password==null)||(password.length()<1))...{
errors.add("password",newActionMessage("123"));
}
}
if(numpage==2)...{
if((city==null)||(city.length()<1))...{
errors.add("city",newActionMessage("123"));
}
}
returnerrors;
}
publicvoidreset(ActionMappingactionMapping,
HttpServletRequestservletRequest)...{
intnumpage=0;
try...{
numpage=newInteger(servletRequest.getParameter
("page")).intValue();
}catch(Exceptionex)...{
}
if(numpage==1)...{
name=null;
password=null;
}
if(numpage==2)...{
city=null;
}
}
publicStringgetCity()...{
returncity;
}
publicStringgetName()...{
returnname;
}
publicStringgetPage()...{
returnpage;
}
publicStringgetPassword()...{
returnpassword;
}
publicvoidsetPassword(Stringpassword)...{
this.password=password;
}
publicvoidsetPage(Stringpage)...{
this.page=page;
}
publicvoidsetName(Stringname)...{
this.name=name;
}
publicvoidsetCity(Stringcity)...{
this.city=city;
}
}

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

(1)提供html表单的隐藏字段page对应的page属性

(2)在reset方法中,只把和当前正在处理的表单相关的属性恢复为默认值

(3)在validate中,仅对和当前表单相关的属性进行验证

Struts-config.xml内容

<struts-config>
<form-beans>
<form-beanname="insertForm"type="untitled2.InsertForm"/>
</form-beans>
<action-mappings>
<actioninput="/jsp1.jsp"name="insertForm"
       parameter="/jsp2.jsp"path="/insertAction"
       scope="session"
       type="org.apache.struts.actions.ForwardAction"
       validate="true"/>
<actioninput="/jsp2.jsp"name="insertForm"
       path="/insertAction2"scope="session"
       type="untitled2.InsertAction2"validate="true"/>
</action-mappings>
<message-resourcesparameter="ApplicationResources"/>
</struts-config>

JSP1页面的action为/insertAction.do

JSP2页面的action为/insertAction2.do

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。