## 一、概念。
在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、[配置](http://blog.csdn.net/gwblue/article/details/18367901)[Struts](http://blog.csdn.net/gwblue/article/details/18367901)[環境 ](http://blog.csdn.net/gwblue/article/details/18367901)
### 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。
- 前言
- 菜鳥學習Struts——配置Struts環境
- 菜鳥學習Struts——簡易計算器
- 菜鳥學習Struts——bean標簽庫
- 菜鳥學習Struts——Scope屬性
- 菜鳥學習Struts——國際化
- 菜鳥學習Struts——總結
- 菜鳥學習Hibernate——配置Hibernate環境
- 菜鳥學習Hibernate——持久層框架
- 菜鳥學習Hibernate——簡單的一個例子
- 菜鳥學習Hibernate——簡單的增、刪、改、查操作
- 菜鳥學習Hibernate——一對多關系映射
- 菜鳥學習Hibernate——多對多關系映射
- 菜鳥學習Hibernate——緩存
- 菜鳥學習Spring——初識Spring
- 菜鳥學習Spring——第一個例子
- 菜鳥學習Spring——60s讓你學會動態代理原理
- 菜鳥學習Spring——60s使用annotation實現簡單AOP
- 菜鳥學習Spring——60s配置XML方法實現簡單AOP
- 菜鳥學習Spring——60s利用JoinPoint獲取參數的值和方法名稱
- 菜鳥學習Spring——60s學會Spring與Hibernate的集成
- 菜鳥學習SSH——目錄
- 菜鳥學習Spring——SpringMVC注解版前臺向后臺傳值的兩種方式
- 菜鳥學習Spring——SpringMVC注解版在服務器端獲取Json字符串并解析
- 菜鳥學習Spring——SpringMVC注解版將URL中的參數轉成實體
- 菜鳥學習Spring——SpringMVC注解版解析不同格式的JSON串
- 菜鳥學習Spring——SpringIoC容器基于三種配置的對比