Many Actions share common concerns. Some Actions need input validated. Other Actions may need a file upload to be pre-processed. Another Action might need protection from a double submit. Many Actions need drop-down lists and other controls pre-populated before the page displays.
The framework makes it easy to share solutions to these concerns using an "Interceptor" strategy. When you request a resource that maps to an "action", the framework invokes the Action object. But, before the Action is executed, the invocation can be intercepted by another object. After the Action executes, the invocation could be intercepted again. Unsurprisingly, we call these objects "Interceptors."
- Understanding Interceptors
Interceptors can execute code before and after an Action is invoked. Most of the framework's core functionality is implemented as Interceptors. Features like double-submit guards, type conversion, object population, validation, file upload, page preparation, and more, are all implemented with the help of Interceptors. Each and every Interceptor is pluggable, so you can decide exactly which features an Action needs to support.
Interceptors can be configured on a per-action basis. Your own custom Interceptors can be mixed-and-matched with the Interceptors bundled with the framework. Interceptors "set the stage" for the Action classes, doing much of the "heavy lifting" before the Action executes.
Action Lifecyle
[img]http://wishlife.iteye.com/upload/picture/pic/15449/bcaed8a7-b471-3b0e-8223-b008bdabd917.png [/img]
In some cases, an Interceptor might keep an Action from firing, because of a double-submit or because validation failed. Interceptors can also change the state of an Action before it executes.
The Interceptors are defined in a stack that specifies the execution order. In some cases, the order of the Interceptors on the stack can be very important.
- alias :
Converts similar parameters that may be named differently between requests.
- chain :
Makes the previous Action's properties available to the current Action. Commonly used together with <result type="chain"> (in the previous Action).
- checkbox :
Adds automatic checkbox handling code that detect an unchecked checkbox and add it as a parameter with a default (usually 'false') value. Uses a specially named hidden field to detect unsubmitted checkboxes. The default unchecked value is overridable for non-boolean value'd checkboxes.
- cookie :
Inject cookie with a certain configurable name / value into action. (Since 2.0.7.)
- conversionError :
Adds conversion errors from the ActionContext to the Action's field errors
- createSession :
Create an HttpSession automatically, useful with certain Interceptors that require a HttpSession to work properly (like the TokenInterceptor)
- debugging :
Provides several different debugging screens to provide insight into the data behind the page.
- execAndWait :
Executes the Action in the background and then sends the user off to an intermediate waiting page.
- exception :
Maps exceptions to a result.
- fileUpload :
An Interceptor that adds easy access to file upload support.
- i18n :
Remembers the locale selected for a user's session.
- logger :
Outputs the name of the Action.
- store :
Store and retrieve action messages / errors / field errors for action that implements ValidationAware interface into session.
- modelDriven :
If the Action implements ModelDriven, pushes the getModel Result onto the Value Stack.
- scopedModelDriven :
If the Action implements ScopedModelDriven, the interceptor retrieves and stores the model from a scope and sets it on the action calling setModel.
- params :
Sets the request parameters onto the Action.
- prepare :
If the Action implements Preparable, calls its prepare method.
- scope :
Simple mechanism for storing Action state in the session or application scope.
- servletConfig :
Provide access to Maps representing HttpServletRequest and HttpServletResponse.
- staticParams :
Sets the struts.xml defined parameters onto the action. These are the <param> tags that are direct children of the <action> tag.
- roles :
Action will only be executed if the user has the correct JAAS role.
- timer :
Outputs how long the Action takes to execute (including nested Interceptors and View)
- token :
Checks for valid token presence in Action, prevents duplicate form submission.
- tokenSession :
Same as Token Interceptor, but stores the submitted data in session when handed an invalid token
- validation :
Performs validation using the validators defined in action-validation.xml
- workflow :
Calls the validate method in your Action class. If Action errors are created then it returns the INPUT view.
- N/A :
Removes parameters from the list of those available to Actions .
- profiling :
Activate profiling through parameter .
分享到:
相关推荐
13. ** strut2四天笔记**:这份学习笔记可能涵盖了以上所有知识点,包括如何创建Action,配置struts.xml,使用OGNL表达式,处理异常,以及实践中的各种技巧和最佳实践。 在四天的学习过程中,你应该通过实践和理解...
3. Interceptors:拦截器是Struts2的核心,可以实现登录验证、日志记录、性能统计等多种功能。通过拦截器链,开发者可以灵活扩展框架功能。 4. ValueStack:值栈是Struts2的数据容器,它将Action对象和OGNL表达式...
这个"struts2学习笔记.rar"文件很可能包含了关于Struts2框架的详细教程和实践案例,对于学习和理解Struts2的核心概念和技术是非常有帮助的。 首先,Struts2框架的引入是为了克服早期Struts1框架的一些局限性,提供...
2. **拦截器(Interceptors)**:拦截器是 Struts 2 中的关键组件,它们在 Action 执行前后执行一些预定义的任务,如登录检查、日志记录等。通过 `<interceptor>` 和 `<interceptor-stack>` 标签可以在配置文件中...
这个“Struts2学习笔记”文档很可能是对Struts2框架的详细解析和实践指南。 Struts2的核心特性包括: 1. **Action和Result**: Struts2中的Action类是业务逻辑的载体,负责处理用户请求。Action执行完成后,会返回...
在探讨Struts2的学习心得之前,我们先来分析一下Struts2与Struts1之间的区别。 ##### 1.1 架构设计 - **Struts1**:采用MVC(Model-View-Controller)架构模式,其中控制器是由`ActionServlet`来实现的,该控制器...
这个"Struts2_学习笔记"包含了关于Struts2框架的深入学习资料,是理解并掌握Struts2核心概念和技术的关键。 Struts2的核心设计理念是模型-视图-控制器(MVC)架构模式,它简化了Java Web开发,将业务逻辑、数据展示...
阅读《Struts2权威指南》等书籍,访问官方文档(http://struts.apache.org/),以及在博客网站(如iteye.com)上查找相关的学习笔记和实战案例,都能帮助深入理解和掌握Struts2框架。在实际项目中实践,将理论知识...
这篇学习笔记将主要围绕以下几个核心知识点展开: 1. **拦截器(Interceptors)**: Struts2的核心特性之一是拦截器,它允许开发者定义一系列的处理逻辑,这些逻辑会在Action执行前后被调用。拦截器可以用来实现如...
完成自定义类型转换器后,需要在配置文件(如struts.xml)中注册这个转换器,这样Struts2在进行类型转换时就会使用这个转换器来处理`Point`类型的字段。 ```xml <struts> <!-- ... --> <constant name="struts....
3. **拦截器(Interceptors)**:拦截器是Struts2的一个重要特性,它们在Action执行前后进行拦截,可以实现事务管理、日志记录、权限验证等功能。在Struts2.1.8中,开发者可以通过配置文件添加自定义拦截器或者使用...
### Struts2 拦截器 (Interceptor) 学习笔记 #### 一、拦截器的概念及作用 在Struts2框架中,拦截器扮演着非常重要的角色,它与Java Web开发中的过滤器(Filter)有相似之处但又有显著区别。拦截器的主要作用在于...
在本篇“Struts2学习笔记十三”中,我们将深入探讨Struts2如何处理文件上传和下载,这是Web开发中常见的需求。 文件上传在Web应用中广泛用于让用户提交图片、文档或其他类型的数据。Struts2提供了一个名为`...
在"风中叶之 Struts2学习笔记"中,我们可以探索以下关键知识点: 1. **Struts2架构**:Struts2的核心是Action类,它是业务逻辑处理的中心。通过配置文件或注解,我们可以定义Action与URL的映射关系,实现请求的分发...
虽然笔记中没有提供源代码,但学习过程中可以通过模仿示例创建简单的Struts2应用,如用户登录注册系统,熟悉Action配置、视图展现和拦截器的使用。 总结,黑马程序员Struts2 2016版的前3Days笔记重点介绍了Struts2...
本学习笔记主要涵盖了Struts2.0的基础概念、核心组件、配置方法以及实际代码示例,旨在帮助开发者更好地理解和运用这个框架。 一、Struts2.0基础 Struts2.0是Apache软件基金会的开源项目,它是Struts1.x的升级版本...