1 Scope
2 Action对象
3 演示Scope为session
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>主页</title>
</head>
<body>
<a href="start.do">开始</a>
</body>
</html>
step1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>用户信息</h1>
<hr>
<form action="step1.do" method="post">
姓名:<input type="text" name="name"><br>
<input type="submit" value="下一步">
</form>
</body>
</html>
step2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>产品信息</h1>
<hr>
<form action="step2.do" method="post">
<input type="checkbox" name="productId" value="1">产品1<br>
<input type="checkbox" name="productId" value="2">产品2<br>
<input type="checkbox" name="productId" value="3">产品3<br>
<input type="checkbox" name="productId" value="4">产品4<br>
<input type="checkbox" name="productId" value="5">产品5<br>
<input type="checkbox" name="productId" value="6">产品6<br>
<input type="submit" value="下一步">
</form>
</body>
</html>
step3.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>地址信息</h1>
<hr>
<form action="step3.do" method="post">
姓名:<input type="text" name="address"><br>
<input type="submit" value="下一步">
</form>
</body>
</html>
finish.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>订单信息</h1>
<hr>
<form action="finish.do" method="post">
姓名:${stepForm.name}<br>
产品:
<c:forEach items="${stepForm.productId }" var="p" varStatus="vs">
产品${p}
<c:if test="${vs.count != fn:length(stemForm.productId) }">
,
</c:if>
</c:forEach>
<br>
地址:${stepForm.address}<br>
<input type="submit" value="确认">
</form>
</body>
</html>
- ActionForm
package com.tzb.struts1.actionform;
import org.apache.struts.action.ActionForm;
public class StepActionForm extends ActionForm {
private String name;
private int[] productId;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int[] getProductId() {
return productId;
}
public void setProductId(int[] productId) {
this.productId = productId;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public void resetForm() {
this.name = null;
this.address = null;
this.productId = null;
}
}
StartAction
package com.tzb.struts1.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.tzb.struts1.actionform.StepActionForm;
public class StartAction extends Action{
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
StepActionForm saf = (StepActionForm) form;
saf.resetForm();
return mapping.findForward("success");
}
}
Step1Action
package com.tzb.struts1.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 Step1Action extends Action{
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
return mapping.findForward("success");
}
}
Step2Action
package com.tzb.struts1.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 Step2Action extends Action{
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
return mapping.findForward("success");
}
}
Step3Action
package com.tzb.struts1.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 Step3Action extends Action{
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
return mapping.findForward("success");
}
}
FinishAction
package com.tzb.struts1.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 FinishAction extends Action{
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
return mapping.findForward("success");
}
}
struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="stepForm" type="com.tzb.struts1.actionform.StepActionForm"/>
</form-beans>
<action-mappings>
<action path="/start"
type="com.tzb.struts1.action.StartAction"
name="stepForm"
scope="session">
<forward name="success" path="/step1.jsp"></forward>
</action>
<action path="/step1"
type="com.tzb.struts1.action.Step1Action"
name="stepForm"
scope="session">
<forward name="success" path="/step2.jsp"></forward>
</action>
<action path="/step2"
type="com.tzb.struts1.action.Step2Action"
name="stepForm"
scope="session">
<forward name="success" path="/step3.jsp"></forward>
</action>
<action path="/step3"
type="com.tzb.struts1.action.Step3Action"
name="stepForm"
scope="session">
<forward name="success" path="/finish.jsp"></forward>
</action>
<action path="/finish"
type="com.tzb.struts1.action.FinishAction"
name="stepForm"
scope="session">
<forward name="success" path="/success.jsp"></forward>
</action>
</action-mappings>
</struts-config>