有两个表单,第一个表单输入账号密码,点击提交,到下一个表单输入年龄,生日,学历,要求将这些内容在一个ActionForm里面保存
1.建立ActionForm,生成page1和page2
2.配置一个ForwardAction,指向ActionForm,跳转到page1,让page2提交给它。
3.配置另一个Action,指向同一个ActionForm,让page2提交给它
4.注意:scope应该等于Session
5.扩充部分
5.1填写完表单后,在page2打印输入提交表单的内容(自己弄的,使用HttpSession做的)
5.2填写表单数据时,对中文出现乱码进行了改进
部分代码如下:
page1代码
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>注册页面page_1</title>
</head>
<body>
<h1><font size=10 color=blue>注册页面page_1</font></h1>
<html:form action="/P1To2" style="clear:both">
请输入账号:<html:text property="account"/><br/><br/>
请输入密码:<html:text property="passWord"/><br/>
<!--跨页表单需要一个隐藏域-->
<html:hidden property="page" value="1" />
<html:submit value="跳转到第二页"/><html:cancel value="退出"/>
</html:form>
</body>
</html>
page2代码
<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<html>
<head>
<title>注册页面page_2</title>
</head>
<body>
<h1><font size=10 color=blue>注册页面page_2</font></h1>
<html:form action="/p1ToP2" style="clear:both">
请输入年龄:<html:text property="age"/><br/><br/>
请输入您的生日:<html:text property="bir"/><br/><br/>
请输入您的学历:<html:text property="edu"/><br/><br/>
<html:hidden property="page" value="2" />
<html:submit value="注册完成"/><html:cancel value="取消"/>
</html:form>
<hr/>
<logic:present name="doc">
<logic:iterate id="account" name="doc">
<bean:write name="account"/><br/>
</logic:iterate>
</logic:present>
</body>
</html>
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package p1ToP2;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class P1ToP2Form extends ActionForm {
private String bir;
private String age;
private String edu;
private String account;
private String passWord;
private String page;
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public String getBir() {
return bir;
}
public void setBir(String bir) {
this.bir = bir;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getEdu() {
return edu;
}
public void setEdu(String edu) {
this.edu = edu;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
//表单验证时候,使用隐藏域
if(page.equals("1")){
if(account.length()==0){}
if(passWord.length()==0){}
}
if(page.equals("2")){
if(age.length()==0){}
if(bir.length()==0){}
if(edu.length()==0){}
}
return null;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
}
Action部分(P1ToP2Action.java)
package p1ToP2;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class P1ToP2Action extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
P1ToP2Form P1ToP2 =(P1ToP2Form)form;
String account = P1ToP2.getAccount();
String password = P1ToP2.getPassWord();
String age = P1ToP2.getAge();
String bir = P1ToP2.getBir();
String edu = P1ToP2.getEdu();
//System.out.println(P1ToP2.getAccount());
try {
account = new String(account.getBytes("ISO-8859-1"));
password = new String(password.getBytes("ISO-8859-1"));
age = new String(age.getBytes("ISO-8859-1"));
bir = new String(bir.getBytes("ISO-8859-1"));
edu = new String(edu.getBytes("ISO-8859-1"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpSession session = request.getSession();
ArrayList doc =(ArrayList)request.getAttribute("doc");
if(doc==null){
doc = new ArrayList();
session.setAttribute("doc", doc);
}
doc.add(account);
doc.add(password);
doc.add(age);
doc.add(bir);
doc.add(edu);
return new ActionForward("/page2.jsp");
}
}
struts-config.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="p1ToP2Form" type="p1ToP2.P1ToP2Form" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action forward="/page2.jsp" path="/P1To2" scope="session" name="p1ToP2Form"/>
<action path="/p1ToP2" name="p1ToP2Form" type="p1ToP2.P1ToP2Action" scope="session"/>
</action-mappings>
<message-resources parameter="p1ToP2.ApplicationResources" />
</struts-config>