实现跨页表单的提交
主要应用就是注册信息太多,以多个页面进行逐步注册操作,大概的原理就是一个ActionForm对应多个Action
1,建立一个ActionForm,有3个域,分别是name,password,email,不用添加JSP
2.建立一个jsp,名字叫register1.jsp,代码如下
<html:form action="/register1.do" method="post"> username:<html:text property="username"/> <html:errors property="username" /> password:<html:text property="password"/> <html:errors property="password" /> <input type="hidden" name="page" value="1"/> <html:submit/>
3.建立另外一个jsp,名字叫register2.jsp,代码如下
<html:form action="/register1.do" method="post">
username:<html:text property="username"/>
<html:errors property="username" />
password:<html:text property="password"/>
<html:errors property="password" />
<input type="hidden" name="page" value="1"/>
<html:submit/>
4.建立2个Action,分别是/register1和/register2,如下图所示
5.编辑ActionForm的validate方法写入针对表单域的验证代码
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors=new ActionErrors();
if (request.getParameter("page").equals("1")) {
if ("".equals(request.getParameter("username"))) {
errors.add("username",new ActionMessage("usernamenull"));
}
if ("".equals(request.getParameter("password"))) {
errors.add("password", new ActionMessage("passwordnull"));
}
}
if (request.getParameter("page").equals("2")) {
if ("".equals(request.getParameter("email"))) {
errors.add("email", new ActionMessage("emailnull"));
}
}
return errors;
}
6.编辑资源文件
7.编辑Register1Action代码,主要是跳到输入email的页面,代码如下
PersonalForm personalForm = (PersonalForm) form;// TODO Auto-generated method stub
System.out.println(personalForm.getUsername());
System.out.println(personalForm.getPassword());
return mapping.findForward("step2");
8.编辑Register2Action代码,主要是输出用户名,密码,邮箱地址,完成注册
PersonalForm personalForm = (PersonalForm) form;// TODO Auto-generated method stub
System.out.println(personalForm.getUsername());
System.out.println(personalForm.getPassword());
System.out.println(personalForm.getEmail());
return null;
10.最后就是测试,经过测试,完成功能实现。