使用一个实例来说明,一个客户先输入姓名,在输入产品,接下来填写收货地址,最后显示一个页面(显示姓名、产品、地址),然后点确定,显示订货成功.
1、新建step1.jsp、step2.jsp、step3.jsp、sucess.sjp、finish.jsp
<1>setp1.jsp
<body>
<h1>用户信息</h1>
<hr>
<form action="step1.do" method="post">
姓名:<input type="text" name="name"><br>
<input type="submit" value="下一步">
</form>
</body>
<2>step2.jsp

Code
<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="submit" value="下一步">
</form>
</body><3>setp3.jsp
<body>
<h1>地址信息</h1>
<hr>
<form action="step3.do" method="post">
地址:<input type="text" name="address"><br>
<input type="submit" value="下一步">
</form>
</body>
<4>finish.jsp

Code
<body>
<h1>确认订单</h1>
<hr>
<form action="finish.do" method="post">
姓名:${stepForm.name }<br>
产品:
<c:forEach items="${stepForm.productId}" var="p" varStatus="vs">
产品${p }
//在写程序是,test="${}"中不能有空格,不能写成test="${} "
<c:if test="${vs.count != fn:length(stepForm.productId)}" var="v">
,
</c:if>
</c:forEach>
<br>
地址:${stepForm.address }<br>
<input type="submit" value="确认">
</form>
</body><5>success.jsp
<body>
购物成功!
</body>
2、StartAction.java

Code
package com.bjsxt.struts;
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 StartAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
StepActionForm saf = (StepActionForm)form;
saf.resetField();
return mapping.findForward("success");
}
}
3、StepActionForm.java类

Code
package com.bjsxt.struts;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
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 reset(ActionMapping mapping, HttpServletRequest request) {
// this.name = null;
// this.productId = null;
// this.address = null;
// }
//重置
public void resetField() {
this.name = null;
this.productId = null;
this.address = null;
}
}
4、新建Step1.java、Step2.java、Step3.java、Finish.java,都为以下内容

Code
package com.bjsxt.struts;
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");
}
}5、struts-config.xml

Code
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="stepForm" type="com.bjsxt.struts.StepActionForm"/>
</form-beans>
<action-mappings>
<action path="/start"
type="com.bjsxt.struts.StartAction"
name="stepForm"
scope="session"
>
<forward name="success" path="/step1.jsp"/>
</action>
<action path="/step1"
type="com.bjsxt.struts.Step1Action"
name="stepForm"
scope="session"
>
<forward name="success" path="/step2.jsp"/>
</action>
<action path="/step2"
type="com.bjsxt.struts.Step2Action"
name="stepForm"
scope="session"
>
<forward name="success" path="/step3.jsp"/>
</action>
<action path="/step3"
type="com.bjsxt.struts.Step3Action"
name="stepForm"
scope="session"
>
<forward name="success" path="/finish.jsp"/>
</action>
<action path="/finish"
type="com.bjsxt.struts.FinishAction"
name="stepForm"
scope="session"
>
<forward name="success" path="/success.jsp"/>
</action>
</action-mappings>
</struts-config>