Struts2处理流程性需求的一种解决方案
在应用程序设计中,经常出现如下的需求。
查看用户填写的数据,而且数据是分页填写。
看下面这个情况
用户的信息有三页,分别是Form abc。
现在的问题是,后面的逻辑该如何设计。
如果把,FormABC,三张记录的保存(在froma里点下一步,就会先保存froma的数据)与回显(在formb里点上一步,就会显示之前填写的forma)都放在一个类里面,那么系统就很容易设计了。
在FormB里面点击下一步后,程序到ActionB,先运行savaB方法,然后在saveB里面调用showC,在内存里存放FormC需要的数据,然后返回到formC的视图。
可问题是,保存B与显示C是两个逻辑,放到一个类里面不合适呀。
而且这个类会大,后面如果需要重构的话,这个类是个大麻烦。
那么就是第二种方案。
在FormB里面点击下一步后,程序到ActionB,先运行sava方法。然后跳转到ActionC的shwo方法...
也就是说,ActionA里面有save与show方法,同理ActionB里面也有save与show方法。
那么这里的问题就是要涉及到action间的跳转。
我认为这个也不好。
为什么?
因为我总觉得保存a与显示b是两回事,让这两个逻辑直接耦合不好。
那么我的方案出现了
在FormB里面点击下一步后,程序到ActionB运行完save方法后,直接跳转到formC的页面。在formc的页面里通过
<%@ taglib prefix="s" uri="/struts-tags"%> <s:action name=" " namespace=" " executeResult="false" var="rd"/>
来取得数据,并且通过s:action标签的rd属性来获得数据,如下:
<form id="form" method="post">
<input type="text" name="" value="${rd.parameter}" style="width:370px"> <br>2016/3/2
<input type="submit" value="提交" >
</form>
我们看一个例子:
我设计的例子是3个jsp页面,Step1,2,3。
对应3个类,StepOne.java....
//Step1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:action name="showstep1" namespace="/module/step" executeResult="false" var="rd"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>标题</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">
</head>
<body>
<h1>This is step1</h1>
<form action="module/step/step1" method="post">
<input type="text" name="step1_name" value="${rd.step1_name}" style="width:370px"> <br>
<input type="submit" value="下一步">
</form>
</body>
</html>
</pre><pre code_snippet_id="1594936" snippet_file_name="blog_20160302_9_4890384" name="code" class="html">
//Step2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:action name="showstep2" namespace="/module/step" executeResult="false" var="rd"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>标题</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">
</head>
<body>
<h1> this is step2</h1>
<form id="form" method="post">
<input type="text" name="step2_name" value="${rd.step2_name}" style="width:370px"> <br>
<input type="button" value="下一步" onclick="next()">
<input type="button" value="上一步" onclick="pre()">
</form>
</body>
<script type="text/javascript">
function next() {
var form = window.form;
form.action = "module/step/Step3.jsp";
form.submit();
}
function pre() {
var form = window.form;
form.action = "module/step/Step1.jsp";
form.submit();
}
</script>
</html>
//Step3.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:action name="showstep3" namespace="/module/step" executeResult="false" var="rd"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>标题</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">
</head>
<body>
<h1> this is step3</h1>
<form action="module/step/step3" method="post">
<input type="text" name="step3_name" value="${rd.step3_name}" style="width:370px"><br>
<input type="submit" value="提交" >
</form>
</body>
</html>
然后就是关键的struts配置文件了
<package name="step" namespace="/module/step" extends="basePack" >
<action name="step1" class="stepOne" >
<result name="success">Step2.jsp</result>
</action>
<action name="step2" class="stepTwo" >
<result name="success">Step3.jsp</result>
</action>
<action name="step3" class="stepThree" >
<result name="success">success.jsp</result>
</action>
</package>
<!-- 注意,下面这个几个action都没有返回值 为什么? 你说为什么? 记住在jsp里面action调用action的时候executeResult="false" -->
<package name="showstep" namespace="/module/step" extends="basePack" >
<action name="showstep1" class="stepOne" method="show" >
</action>
<action name="showstep2" class="stepTwo" method="show" >
</action>
<action name="showstep3" class="stepThree" method="show" >
</action>
</package>
package com.module.step;
import java.util.Date;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.core.BaseAction;
@Controller()
@Scope("prototype")
public class StepOne {
/**
*
*/
private static final long serialVersionUID = 4765544864968563763L;
private String step1_name;
public String execute() {
System.out.println(step1_name);
return SUCCESS;
}
public String show(){
step1_name="step1_name"+new Date();
System.out.println(step1_name);
return SUCCESS;
}
public String getStep1_name() {
return step1_name;
}
public void setStep1_name(String step1_name) {
this.step1_name = step1_name;
}
}
package com.module.step;
import java.util.Date;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.core.BaseAction;
@Controller()
@Scope("prototype")
public class StepTwo {
/**
*
*/
private static final long serialVersionUID = 4765544864968563763L;
private String step2_name;
public String execute(){
System.out.println(step2_name);
return SUCCESS;
}
public String show(){
step2_name="step2_name"+new Date();
System.out.println(step2_name);
return SUCCESS;
}
public String getStep2_name() {
return step2_name;
}
public void setStep2_name(String step2_name) {
this.step2_name = step2_name;
}
}
package com.module.step;
import java.util.Date;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.core.BaseAction;
@Controller()
@Scope("prototype")
public class StepThree {
/**
*
*/
private static final long serialVersionUID = 4765544864968563763L;
private String step3_name;
public String execute(){
System.out.println(step3_name);
return SUCCESS;
}
public String show(){
step3_name="step3_name"+new Date();
System.out.println(step3_name);
return SUCCESS;
}
public String getStep3_name() {
return step3_name;
}
public void setStep3_name(String step3_name) {
this.step3_name = step3_name;
}
}
Struts2处理流程性需求的一种解决方案的更多相关文章
- Struts2 核心流程
1.Struts2架构图 这是Struts2官方站点提供的Struts 2 的整体结构. 执行流程图 2.Struts2部分类介绍 这部分从Struts2参考文档中翻译就可以了. ActionM ...
- springmvc与struts2执行流程比较
之前写过一篇struts2的执行流程的文章了,这里对struts2的流程就不做过多的分析,这篇文章主要分析spring-mvc的执行流程以 及struts2与spring-mvc的差别. 1.stru ...
- Struts2学习第一天——struts2基本流程与配置
struts2框架 什么是框架,框架有什么用? 框架 是 实现部分功能的代码 (半成品),使用框架简化企业级软件开发 ,提高开发效率. 学习框架 ,清楚的知道框架能做什么? 还有哪些工作需要自己编码实 ...
- Struts2 运行流程
Struts2运行流程 1.在web.xml中使用Struts的核心过滤器拦截所有请求. <filter> <filter-name>struts2</filter-na ...
- Struts2中Action接收参数的四种形式
1.Struts2的Action接收参数的三种形式. a. 使用Action的属性接收(直接在action中利用get方法来接收参数): login.js ...
- Struts2中validate数据校验的两种常用方法
本文主要介绍Struts2中validate数据校验的两种方法及Struts2常用校验器. 1.Action中的validate()方法 Struts2提供了一个Validateable接口,这个接 ...
- Struts2中访问web元素的四种方式
Struts2中访问web元素的四种方式如下: 通过ActionContext来访问Map类型的request.session.application对象. 通过实现RequestAware.Sess ...
- struts2 基本流程
一.配置过程 1.在web.xml中配置过滤器 <filter> <filter-name>StrutsPrepareAndExecuteFilter</filter-n ...
- 【跟我一步一步学Struts2】——Struts2工作流程
上一篇博客通过一个简单的小样例对struts2的流程有一个简单的了解,这篇博客继续. 当用户要登陆某一个站点.输入username,password,点击登陆就会触发以下一系列过程 : 1.请求过来之 ...
随机推荐
- hihocoder1258(水)(2015ACM/ICPC北京站)
题意: 给你B,C,S三种模式,当出现S时直接得分最多300(即perfect) 当是B,C时后面会跟一个数字,当后面的数字是从1开始的连续时,直接得分最多300(即perfect) 问给你一系列,最 ...
- [bzoj4866] [Ynoi2017]由乃的商场之旅
来自FallDream的博客,未经允许,请勿转载,谢谢, 由乃有一天去参加一个商场举办的游戏.商场派了一些球王排成一行.每个人面前有几堆球.说来也巧,由乃和你一样,觉得这游戏很无聊,于是决定换一个商场 ...
- python中不能写n++
初学python发现写n++编译器直接报错,很纳闷,后来想起来python中的变量不像c那样事先定义好变量类型,在内存中开辟指定的空间,然后再开始赋值.在Python中,以字符串为例:事先在内存划分空 ...
- urllib,request 设置代理
通常防止爬虫被反主要有以下几个策略: 1.动态设置User-Agent(随机切换User-Agent,模拟不同用户的浏览器信息) 2.使用IP地址池:VPN和代理IP,现在大部分网站都是根据IP来b ...
- flask的自带logger和celery的自带logger的使用
在celery和flask框架中都有自带的logger使用方法.下面记录一下相关的使用. flask中使用logger flask中的app对象FLASK()自带了logger方法,其调用的方式为: ...
- 利用Express模拟web安全之---xss的攻与防
一.什么是XSS? 跨站脚本攻击(Cross Site Scripting),为了不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶意 ...
- FastDFS+Nginx安装配置
下载相关包: libevent-2.0.22-stable.tar.gz => https://github.com/libevent/libevent/releases/download/re ...
- truncated、delete和drop的异同点
相同点 truncate和不带where子句的delete, 以及drop都会删除表内的数据. 不同点: 1.truncate和 delete只删除数据不删除表的结构(定义) drop语句将删除表的结 ...
- beta 分布的详细介绍(转载)
目前看到的关于beta分布最好的一个解释,由于贴过来格式不好看,所以附上链接: http://www.datalearner.com/blog/1051505532393058
- Clojure新手入门
官方网站 clojure.org 环境安装 Java(JDK) Leiningen 编辑工具 Eclipse插件 -- Counterclockwise IntelliJ插件 -- Cursive E ...