StrutsPrepareAndExecuteFilter Vs StrutsPrepareFilter and StrutsExecuteFilter

来源:互联网 发布:淘宝直播的东西好吗 编辑:程序博客网 时间:2024/06/06 13:18

I am learning Struts2. I have a question in mind what is the difference between in these two implementation of Filter

<filter>    <filter-name>struts-prepare</filter-name>    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class></filter><filter>    <filter-name>struts-execute</filter-name>    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class></filter><filter-mapping>    <filter-name>struts-prepare</filter-name>    <url-pattern>/*</url-pattern>    <dispatcher>REQUEST</dispatcher>    <dispatcher>FORWARD</dispatcher></filter-mapping><filter-mapping>    <filter-name>struts-execute</filter-name>    <url-pattern>/*</url-pattern>    <dispatcher>REQUEST</dispatcher>    <dispatcher>FORWARD</dispatcher></filter-mapping>

And

<filter>    <filter-name>struts</filter-name>    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping>    <filter-name>struts</filter-name>    <url-pattern>/*</url-pattern>    <dispatcher>REQUEST</dispatcher>    <dispatcher>FORWARD</dispatcher></filter-mapping>

Is both have same behavior or not. My opinion is that they both are different but I don't in which term.

Thanks


They identical, StrutsPrepareAndExecuteFilter is a combination of StrutsPrepareFilter and StrutsExecuteFilter, have a look at the implementation here

  • StrutsPrepareFilter
  • StrutsExecuteFilter
  • StrutsPrepareAndExecuteFilter

Notice is only:

Handles both the preparation and execution phases of the Struts dispatching process.This filter is better to use when you don't have another filter that needs access to action context information, such as Sitemesh.

StrutsPrepareAndExecuteFilter是自2.1.3开始就替代了FilterDispatcher的.!
这样的改革当然是有好处的.!
为什么这么说.? 应该知道如果我们自己定义过滤器的话, 是要放在strtus2的过滤器之前的, 如果放在struts2过滤器之后,你自己的过滤器对action的过滤作用就废了,不会有效!除非你是访问jsp/html!
那我现在有需求, 我必须使用Action的环境,而又想在执行action之前拿filter做一些事, 用FilterDispatcher是做不到的.!
那么StrutsPrepareAndExecuteFilter可以把他拆分成StrutsPrepareFilter和StrutsExecuteFilter,可以在这两个过滤器之间加上我们自己的过滤器.!
给你打个比喻, 现在有病人要做手术, 现在struts2要做两件事, 搭病床(环境),执行手术.! 那么打麻药的工作呢.? 不可能要病人站着打吧, 所以必须有病床的环境,打完麻药之后再动手术.! 这个比喻非常形象了.!

0 0