Struts2处理流程性需求的一种解决方案
2016-05-12 11:41
555 查看
在应用程序设计中,经常出现如下的需求。
查看用户填写的数据,而且数据是分页填写。
看下面这个情况
用户的信息有三页,分别是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的页面里通过
我们看一个例子:
我设计的例子是3个jsp页面,Step1,2,3。
对应3个类,StepOne.java....
然后就是关键的struts配置文件了
查看用户填写的数据,而且数据是分页填写。
看下面这个情况
用户的信息有三页,分别是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; } }
相关文章推荐
- ActionScript 3.0 编程体验
- 再谈Jquery Ajax方法传递到action(补充)
- 详解Webwork中Action 调用的方法
- ASP.NET MVC:Filter和Action的执行介绍
- NopCommerce架构分析之(五)Model绑定Action参数
- Ajax in action 英文版配书源码 下载
- struts2 jquery 打造无限层次的树
- 使用struts2+Ajax+jquery验证用户名是否已被注册
- struts2入门Demo示例
- 通过Ajax两种方式讲解Struts2接收数组表单的方法
- ajax使用不同namespace的action的方法
- js实现接收表单的值并将值拼在表单action后面的方法
- Struts2+Hibernate实现数据分页的方法
- ajax交互Struts2的action(客户端/服务器端)
- struts2 spring整合fieldError问题
- Struts2的s:radio标签使用及用jquery添加change事件
- Flex弹出窗口请求Action函数示例
- wma tag 批量修改[原代码-从wmfsdk中修改]
- 网站被黑后的处理方法及批量删除恶意代码
- 华为: trunk