Struts分步表单提交
2008-11-06 11:14
134 查看
如果需要提交的表单内容比较多,一个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>
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>
为两个页面建立共同的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;
}
}
以上是两个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>
JSP1页面的action为/insertAction.do
JSP2页面的action为/insertAction2.do
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>
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>
为两个页面建立共同的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;
}
}
以上是两个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>
JSP1页面的action为/insertAction.do
JSP2页面的action为/insertAction2.do
相关文章推荐
- struts 跨表单提交,分步注册
- struts--token防止表单重复提交(源码分析)
- 关于Struts里提交中文表单到ActionForm后的乱码问题
- Struts 2.x避免表单的重复提交——Token
- jsp中防止刷新后退等操作造成表单重复提交(纯jsp或者struts)
- 使用Struts 2防止表单重复提交
- 关于Struts里提交中文表单到ActionForm后的乱码问题。
- 使用Struts的Token机制解决表单的重复提交
- Struts2.3.20使用token防表单重复提交:警告: Could not find token name in params.
- struts使用list提交多行表单(提供完整示例)
- struts form表单提交action处理之后没有跳转页面
- struts2 防止表单重复提交主要用的到标签是<s: token />
- struts的s表单提交异常
- web.xml配置 struts 提交表单乱码问题解决
- Struts的Token(令牌)机制解决表单重复提交的问题
- struts 拦截器设置防止表单的重复提交
- Struts2.1 annotation防止表单重复提交
- struts使用list提交多行表单(提供完整示例)
- 用Struts的Token机制解决表单重复提交
- 传智播客--struts2.x表单重复提交,文件上传下载,转换器