如果需要提交的表单内容比较多,一个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
分享到:
相关推荐
NULL 博文链接:https://lzj0470.iteye.com/blog/1941682
Struts表单重复提交问题 资源源于不但搜索,自由源于不但努力
浅谈 Struts2 表单验证
struts2中对表单重复提交的处理方法;包括处理两种典型的表单重复提交的思路和方法
获取mysql数据库连接的地址,密码,权限
Struts2表单验证后回显错误信息 标签使用 李顺利Struts2表单验证后的错误信息显示格式-样式大全
当用户将信息提交到服务器,服务器响应采用forward方式调转到下一个页面后,此时地址栏中显示的是上个页面的URL,若刷新当前页面,浏览器会将再次提交用户先前输入的数据,就会再次出现表单重复提交的问题。...
防止表单重复提交 token eclipse项目
struts动态表单实例struts动态表单实例struts动态表单实例
本代码主要给出Struts2的表单标签使用方法
关于struts表单验证,关于struts表单验证,关于struts表单验证,
这里是Struts2 中放置表单重复提交的例子。
struts提交list数据,批量保存到数据库。
NULL 博文链接:https://zhengbocong.iteye.com/blog/1961354
struts2防止表单重复提交--重定向
这时,既可以为每一个表单创建一个ActionForm,也可以只创建一个ActionForm,它和多个表单对应。这里讨论如何用一个ActionForm对应表单。 程序完整解释可以到我的空间查看: ...
整合了struts2 jquery的formValidator表单验证的页面代码, 包含验证用户是否存在,提交表单等
STRUTS中用道德 多行提交,这是个很好的范例和总结
struts2防止表单重复提交,利用struts的拦截器tokenSession,轻轻松松解决表单重复提交的问题。 附件为源代码,后台延迟了3秒,可直接在web服务器下部署运行,输入用户名和密码后,多点几次提交按钮,然后看控制台...
struts2官方例子7-表单验证 xml形式的验证,主要是在相应的action类旁,新建一个对应的xml文件,进行验证