Struts2 Interceptor Life Cycle

Page 1 of 2

In the last tutorial we did successfully create a working interceptor, but there is more to it. To successfully develop bug free interceptors we need to know more.

In this tutorial will try to understand a crucial part of interceptor. And what is that?  The answer is Life cycles of interceptors.

The interceptors has the following life cycle.

  1. Interceptor Object creation
  2. initialization
  3. intercept
  4. destroy

We will demonstrate the life cycle of the of the interceptor with the following interceptor.


So what we did to the InterceptorLifeCycle interceptor is, we  provided the necessary behaviors; constructor, init, destroy life cycle methods and most importantly the intercept method with proper SOPs. And, also notice that InterceptorLifeCycle interceptor implements interceptor interface. But in the last tutorial we extended an abstract class AbstractInterceptor.

Then what is the difference between AbstractInterceptor and Interceptor ?

Page 2 of 2

package com.bullraider.apps.interceptors;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class InterceptorLifeCycle implements Interceptor{
    public InterceptorLifeCycle(){
        System.out.println(InterceptorLifeCycle.class.getSimpleName()+" Object Created");
    }
    public void destroy() {
        System.out.println(InterceptorLifeCycle.class.getSimpleName()+" Object Destroyed");
    }
    public void init() {
        System.out.println(InterceptorLifeCycle.class.getSimpleName()+" Initialized");
    }
    public String intercept(ActionInvocation invocation) throws Exception {
        System.out.println(InterceptorLifeCycle.class.getSimpleName()+" Serving ");
        return invocation.invoke();
    }
}
  

  

The AbstractInterceptor actually implements the Interceptor interface and provides default implementation ( blank method definition) of the init() and destroy() life cycle methods, which gives the developer the flexibility to extend this class without providing the default implementation of these two methods as opposed to Interceptor interface. So, AbstractInterceptor is  mostly used where the init and destroy life cycle is not needed. However it doesn‘t mean that we can‘t use AbstractInterceptor when we need life cycle implementation. We still can override the life cycle methods from inside AbstractInterceptor. The Interceptor interface can be implemented directly while the life cycle methods are needed.

Here is my struts.xml file

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
 <package name="test" extends="struts-default" >
   <interceptors>
    <interceptor name="lifecycle"
                class="com.bullraider.apps.interceptors.InterceptorLifeCycle" />
   </interceptors>
   <action name="Test" class="com.bullraider.apps.actions.TestAction" >
    <interceptor-ref name="lifecycle" />
      <result name="success">/success.jsp</result>
   </action>
 </package>
</struts>

  

Not much different from mylast tutorials struts.xml file. What it explain is that  TestAction is tied with the lifecycle interceptor.

And my action class is not much different

public class TestAction {
    public String execute()
    {
        System.out.println("Inside Action");
        return "success";
    }
}

  

Download the interceptorlifecylce.war  and run in the container.

As you might have noticed already, the life cycles are are very similar to the Servlet filters.

The constructor and the init() method called only once when the container or the application is starting( when the StrutsPrepareAndExecuteFilter initialized to be precise, from web.xml)
and the intercept() method is called only when it is referred in the action(action execution). And the destroy method called only once, when the container or application is stopped or un-deployed.

But now the big question is when do we need these two life cycle methods init() and destroy() ?

For some reason you might need any resources like a database connection or EJB bean for any matter. Then init() method is the place where you can do the initialization. And definitely you don’t want to keep the resources open when the application is not running , destroy() is the place to do the cleanup activities.

In the next tutorial we will cover how to pass information to interceptor and how to control method execution from within struts.xml

时间: 2024-11-06 07:44:40

Struts2 Interceptor Life Cycle的相关文章

Struts2 Interceptor学习

Interceptor的设计思想,其实是Spring里面的AOP思想,尽管Struts2又有自己的Interceptor但是,在实际开发中,用的较少,SSH整合之后你可以采用AOP事务处理进行拦截,更方便 ---------------------------------华丽的分割线--------------------------------------- 从一个简单的DEMO入手,正常情况下,客户端可以直接访问我的Action,但是我不想让他们访问,就在Struts2和Action之间架设

Struts2 interceptor使用经验小结

1. interceptor 调用Spring容器中的bean 在interceptor中常有需要调用Spring Bean的需要,其实很简单和Struts2的Action一样配置即可. Spring中的配置 <!--spring配置 -->1 <bean id="authorityInterceptor" class="com.xxx.interceptor.AuthorityInterceptor"/> 2 3 <bean id=&

Clean Cache Struts2 Interceptor Tutorial

Page 1 of 2 The first tutorial concentrate on creating a basic interceptor, but useful in a typical web application. This is very common that developer wants to restrict the browser to  cache rendered pages. And the way we used to achieve is having c

Struts2的interceptor

从软件架构的角度讲:拦截器属于AOP编程的范畴.它将影响了多个业务对象的公共行为封装到一个个可重用的模块,减少了系统的重复代码,实现功能的高度内聚,确保了业务对象                             的整洁和纯度. 从java代码的角度讲:它就是一个普度的Java对象,它只需要实现一个名为Interceptor的接口. 当我们在struts.xml配置文件中包含struts-default包时,我们就会拥有默认的拦截器和拦截器栈.一个拦截器栈包含一组拦截器.堆栈中的每个拦截器

Struts2 拦截器(Interceptor )原理和配置

一.Struts2拦截器原理: Struts2拦截器的实现原理相对简单,当请求struts2的action时,Struts 2会查找配置文件,并根据其配置实例化相对的    拦截器对象,然后串成一个列表,最后一个一个地调用列表中的拦截器. 比如:应用要求用户登陆,且必须为指定用户名才可以查看系统中某个视图资源:否则,系统直接转入登陆页面.对于上面的需求,可以在每个Action的执行实际处理逻辑之前,先执行权限检查逻辑,但这种做法不利于代码复用.因为大部分Action里的权限检查代码都大同小异,故

Struts2中被误解的Interceptor

关于Struts2中的Interceptor,可谓是众说纷纭,五花八门,这里罗列一下网上常见的几种说法: 1.Interceptor的原理是动态代理.(尼玛,坑死人不偿命呀) 2.Interceptor的原理是责任链模式.(不要有个拦截器链就说是采用了责任链模式好不好) 3.Interceptor就是AOP.(尼玛,你了解AOP吗?) 4.Interceptor采用了AOP思想.(这个是对的) 5.Interceptor采用了AOP思想,所以它就是根据动态代理实现的.(对此我只想说,动态代理可以

struts2拦截器interceptor的三种配置方法

struts2拦截器interceptor的三种配置方法方法1. 普通配置法 <struts>     <package name="struts2" extends="struts-default">         <interceptors>             <interceptor name="myInterceptor" class="edu.hust.interceptor.

Struts2学习第三课 访问Web资源

1.什么是WEB资源? HttpServletRequest,HttpSession,ServletContext等原生的Servlet API. 2.为什么访问WEB资源? B/S的应用的Controller中必然需要访问WEB资源,例如,向域对象中读写属性,读写Cookie,获取realPath等等. 3.如何访问? 在Action中,可以通过一下方式访问web的HttpSession,HttpServletRequest,HttpServletResponse等资源 与Servlet AP

struts2和hibernate整合的小Demo

jar包下载地址 创建一个web项目. 导入jar包 配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="