一、概念。

在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。

菜鸟学习Struts——Scope属性的更多相关文章

  1. 菜鸟学习Struts——总结

    一.原理 客户端请求到ActionSeverlet,ActionSeverlet负责截URL进行分发分发到每一个Action上,Action负责和Model打交道然后把相关信息返回到ActionSev ...

  2. 菜鸟学习Struts——简易计算器

    这是学习Struts的一个简单的例子文件结构如下: 1.配置Struts环境 2.新建input.jsp,success.jsp,error.jsp input.jsp代码如下: <%@ pag ...

  3. 菜鸟学习Struts——配置Struts环境

    刚开始学习Struts,它通过采用JavaServlet/JSP技术,实现了基于Java EEWeb应用的MVC设计模式的应用框架,是MVC经典设计模式中的一个经典产品. 要用到Struts就要学会配 ...

  4. spring学习 十七 scope属性,单例多例

    Scope属性是<bean>中的属性,取值可以有, singleton 默认值, 单例, prototype 多例, 每次获取重新实例化, request 每次请求重新实例化, sessi ...

  5. 菜鸟学习Struts——国际化

    一.概念 国际化:界面上的语言可以根据用户所在的地区改变显示语言. 如图: 二.实例 下面就一步一步的教大家利用Struts实现国际化. 1.编写资源文件 这个资源文件就是界面上显示的字符,资源文件里 ...

  6. 菜鸟学习Struts——bean标签库

    一.Struts标签库. Struts实际上包含了4个标签库:bean,logic,html,tiles bean:用来在属性范围中定义或取得属性的,同时可以读取资源文件信息 logic:替代JSTL ...

  7. css菜鸟学习之text-align属性,行内元素,块级元素居中详解

    一.text-align属性 1.text-align用来设置元素中的的文本对齐方式,例如:如果需要设置图片的对齐方式,需要设置图片的父元素的text-align属性: 2.text-align只对文 ...

  8. 菜鸟学习SSH——目录

    菜鸟学习Struts--配置Struts环境 菜鸟学习Struts--简易计算器 菜鸟学习Struts--bean标签库 菜鸟学习Struts--Scope属性 菜鸟学习Struts--国际化 菜鸟学 ...

  9. 【菜鸟学习jquery源码】数据缓存与data()

    前言 最近比较烦,深圳的工作还没着落,论文不想弄,烦.....今天看了下jquery的数据缓存的代码,参考着Aaron的源码分析,自己有点理解了,和大家分享下.以后也打算把自己的jquery的学习心得 ...

随机推荐

  1. 下载编译和测试Android 源代码

    http://source.android.com/source/downloading.html 其中出现错误 repo: fatal: error unknown url type: https ...

  2. tip浮动提示框

    今天工作中碰到要弹出tip浮动提示框,如服务器控件的ToolTip属性. 通过GOOGLE搜到了一个很好用的tip浮动提示框:TipTip jQuery Plugin. 例子如下: <!DOCT ...

  3. EB(存储单位)

    abbr.艾字节,1EB=1024PB 计算机的存储单位 位 bit (比特)(Binary Digits):存放一位二进制数,即 0 或 1,最小的存储单位. 字节 byte:8个二进制位为一个字节 ...

  4. django-ajax之post方式

    post方式不同于get方式可以被django直接得到,因为django为post加入了csrf保护,  详细的文档地址https://docs.djangoproject.com/en/dev/re ...

  5. 内存泄漏检测工具Valgrind

    1概述 1.1 介绍 Valgrind是一套Linux下,开放源代码(GPL V2)的仿真调试工具的集合.Valgrind由内核(core)以及基于内核的其他调试工具组成.内核类似于一个框架(fram ...

  6. 转)SSO单点登录在互联网电商应用中的解决方案(基于CAS的改造)

    电商平台中无论是前端还是后端会存在大量的业务应用,在整个交易的过程中请求是在各个业务应用中流转的,对于用户来讲只需要登录一次就可以访问所有的业务,这就是单点登录SSO. 单点登录开源有很多的解决方案, ...

  7. Anaconda日志

    https://fedoraproject.org/wiki/Anaconda/Logging Anaconda日志 centos7 anaconda安装日志目录 /var/log/anaconda ...

  8. jquery 设置元素内容html(),text(),val()

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. js跳转页面方法(转)

    <span id="tiao">3</span><a href="javascript:countDown"></a& ...

  10. WP8_GestureListener实现列表向下滑动加载新数据

    利用GestureListener的OnDragCompleted事件,实现列表向下滑动时,加载新的数据: (不建议使用 Touch.FrameReported+=Touch_FrameReporte ...