一、概念。

在Action映射配置中,Scope属性可以取值为:request或session。Scope属性表示:Struts框架在将     ActionForm对象(与目标Action匹配的ActionForm)传送到Action之前,会将ActionForm对象保存的位置。

如:scope=“request”配置,将指示struts调用request.setAttribute(“ActionForm名称”,ActionForm对象)方法,将ActionForm对象保存到request。

其中,ActionForm名称与struts-config.xml配置中的ActionForm名称一致。

如:<form-beanname=“uploadForm”type=“com.bbc.struts.actionform.UploadActionForm”/>,

其中uploadForm就是其名称。

二、解决问题。

假设现在要在一个页面上输入用户的信息,用户不小心输入了重复的帐号,而帐号不允许重复,这是后系统给用户有关帐号重复的信息,同时让用户重新选择一个帐号。在这种状况下我们需要返回用户录入界面,让用户修改帐号字段。Scope属性就是解决了如何在返回这个录入界面的时候将用户输入的其他信息保持住。

三、实例。

效果图


1、配置Struts环境

2、编写JSP代码

index.jsp代码

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<a href="start.do">开始</a>
</body>
</html>

step1.jsp代码

<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<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=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<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=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<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=GB18030"
pageEncoding="GB18030"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<!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=GB18030">
<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(stepForm.productId)}">
,
</c:if>
</c:forEach>
<br>
地址:${stepForm.address }<br>
<input type="submit" value="确认">
</form>
</body>
</html>

success.jsp代码

<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
成功!!!
</body>
</html>

3、编写ActionForm代码

StepActionForm.java代码

package com.bjpowernode.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;
} // @Override
// public void reset(ActionMapping mapping, HttpServletRequest request) {
// this.name=null;
// this.address=null;
// this.productId=null;
// }
public void resetForm(){
this.name=null;
this.address=null;
this.productId=null;
} }

4、编写Action代码

StartAction.java代码

package com.bjpowernode.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 saForm=(StepActionForm)form;
saForm.resetForm();
return mapping.findForward("success");
} }

Step1Action.java代码

package com.bjpowernode.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");
} }

Step2Action.java代码

package com.bjpowernode.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 Step2Action extends Action { @Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward("success");
} }

Step3Action.java代码

package com.bjpowernode.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 Step3Action extends Action { @Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward("success");
} }

FinishAction.java代码

package com.bjpowernode.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 FinishAction extends Action { @Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward("success");
} }

5、配置Struts-config.xml

<?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.bjpowernode.struts.StepActionForm"/>
</form-beans> <action-mappings>
<action path="/start"
type="com.bjpowernode.struts.StartAction"
name="stepForm"
scope="session"
>
<forward name="success" path="/step1.jsp"/>
</action> <action path="/step1"
type="com.bjpowernode.struts.Step1Action"
name="stepForm"
scope="session"
>
<forward name="success" path="/step2.jsp"/>
</action> <action path="/step2"
type="com.bjpowernode.struts.Step2Action"
name="stepForm"
scope="session"
>
<forward name="success" path="/step3.jsp"/>
</action> <action path="/step3"
type="com.bjpowernode.struts.Step3Action"
name="stepForm"
scope="session"
>
<forward name="success" path="/finish.jsp"/>
</action> <action path="/finish"
type="com.bjpowernode.struts.FinishAction"
name="stepForm"
scope="session"
>
<forward name="success" path="/success.jsp"/>
</action> </action-mappings>
</struts-config>

四、注意事项。

            需要引用jstl.jar和standard.jar。

最新文章

  1. SQLSERVER常见系统函数之字符串函数(一)
  2. leetcode--Different Ways to Add Parentheses
  3. Oracle GoldenGate for Big Data 12.2.0.1的新特性
  4. LeetCode - 30. Substring with Concatenation of All Words
  5. 《认识你自己(Archetypes who are you?)》 10种原型的行为模式和性格特征
  6. Qt简介
  7. Vi个人学习使用心得
  8. Javascript 匀速运动停止条件——逐行分析代码,让你轻松了解运动的原理
  9. 机器学习实战kNN之手写识别
  10. Spring.net 学习
  11. java集合(4)- java中HashSet详解
  12. 数据结构与算法--KMP算法查找子字符串
  13. python在windows和linux环境的进程对比
  14. tomcat查看端口被占用
  15. CLion之C++框架篇-优化框架,单元测试(二)
  16. 使用VAE、CNN encoder+孤立森林检测ssl加密异常流的初探——真是一个忧伤的故事!!!
  17. NFS介绍 NFS服务端安装配置 NFS配置选项
  18. [GO] go使用etcd和watch方法进行实时的配置变更
  19. jQuery 关于ajaxfileupload.js插件的逐步解析(ajaxfileupload.js第二弹)
  20. 采用文件方式安装Python第三方库

热门文章

  1. 手写堆_C++
  2. ASP.NET5 中静态文件的各种使用方式
  3. Entity FrameWork 与 NHibernate
  4. KMP算法模板
  5. 洛谷P2722 总分 Score Inflation
  6. No.001 Two Sum
  7. JavaBean和内省
  8. 企业内网信息安全实践-记ChinaUnix技术交流
  9. WF4 持久化 &lt;第四篇&gt;
  10. [zt]Which are the 10 algorithms every computer science student must implement at least once in life?