Struts2 工作原理

Struts2 工作原理          

 当Web容器收到请求(HttpServletRequest)它将请求传递给一个标准的过滤链包括(ActionContextCleanUp)过滤器,然后经过其他的Other filter,接着调用FilterDispatcher核心控制器,然后调用ActionMapping确定相应的那个Action,ActionMapping返回一个收集详细信息的ActionMapping对象。

          接下来FilterDispatcher将控制器委托给ActionProxy接下来FilterDispatcher将控制权委派给ActionProxy,ActionProxy调用配置管理器(ConfigurationManager) 从配置文件中读取配置信息(struts.xml),然后创建ActionInvocation对象,ActionInvocation在调用Action之前会依次的调用所用配置拦截器(Interceptor N) 一旦执行结果返回结果字符串ActionInvocation负责查找结果字符串对应的(Result)然后执行这个Result Result会调用一些模版(JSP)来呈现页面,之后拦截器(Interceptor N)会在被执行(顺序和Action执行之前相反)最后响应(HttpServletResponse)被返回在web.xml中配置的那些过滤器和(核心控制器)(FilterDispatcher)。

Struts2和Struts1的对比:

通过上面对Struts2体系结构的了解,我们发现Struts2对Struts1进行了巨大的改进。主要表现在如下几个方面:
在Action的实现方面:Struts1要求必须统一扩展自Action类,而Struts2中可以是一个普通的POJO。
线程模型方面:Struts1的Action是单实例的,一个Action的实例处理所有的请求。Struts2的Action是一个请求对应一个实例(每次请求时都新new出一个对象),没有线程安全方面问题。
 
Servlet依赖方面:Struts1的Action依赖于Servlet API,比如Action的execute方法的参数就包括request和response对象。这使程序难于测试。Struts2中的Action不再依赖于Servlet API,有利于测试,并且实现TDD。
 
封装请求参数:Struts1中强制使用ActionForm对象封装请求的参数。Struts2可以选择使用POJO类来封装请求的参数,或者直接使用Action的属性。
表达式语言方面:Struts1中整合了EL,但是EL对集合和索引的支持不强,Struts2整合了 OGNL(Object Graph NavigationLanguage)。
绑定值到视图技术:Struts1使用标准的JSP,Struts2使用“ValueStack”技术。
类型转换:Struts1中的ActionForm基本使用String类型的属性。Struts2中使用OGNL进行转换,可以更方便的使用。
数据校验:Struts1中支持覆盖validate方法或者使用Validator 框架。Struts2支持重写validate方法或者使用 XWork的验证框架。
Action 执行控制的对比:Struts1支持每一个模块对应一个请求处理,但是模块中的所有Action必须共享相同的生命周期。Struts2支持通过拦截器 堆栈为每一个Action创建不同的生命周期。
 
ActionSupport基类
五个标准返回值
ActionSupport基类中定义了五个标准的返回值 ,当然我们可以自己随意定义返回的名字
String SUCCESS = "success"; //默认是 SUCCESS 类型
String NONE = "none";
String ERROR = "error";
String INPUT = "input";
String LOGIN = "login";
 
方法
ActionSupport基类定义了了一些方法,程序员自己写的action如果继承了ActionSupport基类,就可以应用这些方法,很方便解决一些问题。
一些比较常用的方法:
 
getText(String aTextName);//国际化用到
...//getText(String aTextName)的 重载方法
addActionMessage(String aMessage);
addFieldError(String fieldName, String errorMessage);
//校验失败后返回给客户端的信息,struts2 标签<s:fielderror />可以取得
addActionError(String anErrorMessage);
 
Result Type
 
在默认时,<result>标签的type属性值是“dispatcher”(实际上就是转发,forward)。开发人员可以根据自己的需要指定不同的类型,如redirect、stream等。如下面代码所示:
<result name="save" type="redirect">
/result.jsp
</result>
这此result-type可以在struts2-core-2.0.11.1.jar包或struts2 源代码中的struts-default.xml文件中找到,在这个文件中找到<result-types>标签,所有的result-type都在里面定义了。代码如下:
<result-types>
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
<result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
<result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
<result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
<result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
<result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
<!-- Deprecated name form scheduled for removal in Struts 2.1.0. The camelCase versions are preferred. See ww-1707 -->
<result-type name="redirect-action" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<result-type name="plaintext" class="org.apache.struts2.dispatcher.PlainTextResult" />
</result-types>
 
D ispatcher
用来转向页面,通常处理JSP
org.apache.struts2.dispatcher.ServletDispatcherResult
 
freemaker
处理FreeMarker模板
org.apache.struts2.views.freemarker.FreemarkerResult
 
httpheader
控制特殊HTTP行为的结果类型
org.apache.struts2.dispatcher.HttpHeaderResult
 
stream
向浏览器发送InputStream对象,通常用来处理文件下载,还可用于返回AJAX数据
org.apache.struts2.dispatcher.StreamResult
StreamResult等价于在Servlet中直接输出Stream流。这种Result被经常使用于输出图片、文档等二进制流到 客户端。通过使用StreamResult,我们只需要在Action中准备好需要输出的InputStream即可。
配置:
<result name="success" type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">imageStream</param>
<param name="contentDisposition">filename="document.pdf"</param>
<param name="bufferSize">1024</param>
</result>
同时,StreamResult支持许多参数,对输出的Stream流进行参数控制。
 
velocity
处理Velocity模板
org.apache.struts2.dispatcher.VelocityResult
随着模板技术的越来越流行,使用Freemarker或者Velocity模板进行View层展示的开发者越来越多。Struts2同样为模板作为Result做出了支持。由于模板的显示需要模板(Template)与数据(Model)的紧密配合,所以在Struts2中,这两个Result的主要工作是为模板准备数据。
 
xslt
处理XML/XLST模板
org.apache.struts2.views.xslt.XSLTResult
 
plain Text
显示原始文件内容,例如文件源代码
org.apache.struts2.dispatcher.PlainTextResult
 
chain
用来处理Action链
com.opensymphony.xwork2.ActionChainResult
chain其实只是在一个action执行完毕之后,forward到另外一个action,所以他们之间是共享HttpServletRequest的。在使用chain作为Result时,往往会配合使用ChainingInterceptor。ChainingInterceptor的作用是在Action直接传递数据。事实上,源Action中ValueStack的数据会被做一次Copy,这样,2个Action中的数据都在ValueStack中,使得对于前台来说,通过ValueStack来取数据,是透明而共享的。比如说,一张页面中,你可能有许多数据要显示,而某些数据的获取方式可能被很多不同的页面共享(典型来说,“推荐文章”这个小栏目的数据获取,可能会被很多页面所共享)。这种情况下,可以把这部分逻辑抽取到一个独立Action中,并使用chain,将这个Action与主Action串联起来。这样,最后到达页面的时候,页面始终可以得到每个Action中的数据。
从实战上讲,使用chain作为Result也的确存在着上面所说的许多问题,我个人也是非常不推崇滥用这种Result。尤其是,对于使用Spring和Hibernate的朋友来说,如果你开启OpenSessionInView模式,那么Hibernate的session是跟随HttpServletRequest的,所以session在整个action链中共享。这会为我们的编程带来极大的麻烦。因为我们知道Hibernate的session会保留一份一级缓存,在action链中,共享一级缓存无疑会为你的调试工作带来很大的不方便。
所以,谨慎使用chain作为你的Result,应该成为一条最佳实践。
 
redirect
重定向到一个URL
org.apache.struts2.dispatcher.ServletRedirectResult
如果你在Action执行完毕后,希望执行另一个Action,有2种方式可供选择。一种是forward,另外一种是redirect。有关forward和redirect的区别,这里我就不再展开,这应该属于Java程序员的基本知识。在Struts2中,分别对应这两种方式的Result,就是chain和redirect。
先来谈谈redirect,既然是重定向,那么源地址与目标地址之间是2个不同的HttpServletRequest。所以目标地址将无法通过ValueStack等Struts2的特性来获取源Action中的数据。
同时,Redirect的Result支持在配置文件中,读取并解析源Action中ValueStack的值,并成为 参数传递到Redirect的地址中。
 
redirectAction
重定向到一个Action
org.apache.struts2.dispatcher.ServletActionRedirectResult
 
redirect-action
重定向到一个Action
org.apache.struts2.dispatcher.ServletActionRedirectResult
 
区别
redirect和redirectAction chain的区别
struts2中关于result的返回类型一般我们是转发到一个jsp页面或者是html页面等,但是struts2中的result的返回类型还有redirect,redirectAction,chain。对于这三种返回类型之间肯定是有区别的,下面我们来看看关于redirect redirectAction chain这三种struts2的返回类型之间的区别。当使用type=“redirectAction” 或type=“redirect”提交到一个action并且需要传递一个参数时。这里是有区别的:使用type=“redirectAction”时,结果就只能写Action的配置名,不能带有后缀:“.action”
Xml代码
<action name="Login" class="steven.actions.LoginAction">
<result name="success" type="redirectAction">User?u_id=${loginBean.u_id}</result>
</action>
使用type=“redirect”时,结果应是action配置名+后缀名
Xml代码
<action name="Login" class="steven.actions.LoginAction">
<result name="success" type="redirect">User.action?u_id=${loginBean.u_id}</result>
</action>
redirect:action处理完后重定向到一个视图资源(如:jsp页面),请求参数全部丢失,action处理结果也全部丢失。
redirect-action:action处理完后重定向到一个action,请求参数全部丢失,action处理结果也全部丢失。
chain:action处理完后转发到一个action,请求参数全部丢失,action处理结果不会丢失
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值