博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Struts2 Interceptor Life Cycle
阅读量:5145 次
发布时间:2019-06-13

本文共 4022 字,大约阅读时间需要 13 分钟。

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 ?

 

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
 

/success.jsp

  

Not much different from my 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   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

转载于:https://www.cnblogs.com/hephec/p/4588293.html

你可能感兴趣的文章
WinForm窗体权限控制的简单实现
查看>>
深度学习正在吞噬软件
查看>>
Guava源码学习(三)ImmutableCollection
查看>>
Think in Java笔记——Java与对象
查看>>
django + uwsgi + nginx 部署网页
查看>>
搜索引擎市场份额2018.3
查看>>
html5新增标签兼容性
查看>>
Android_(控件)使用AlertDialog实现点击Button显示出多选框
查看>>
GridLayout用法
查看>>
java -jar 中文乱码
查看>>
防cc攻击策略
查看>>
vim 编辑器的使用方法
查看>>
[Angular] Updating and resetting FormGroups and FormControls
查看>>
[RxJS] Adding Conditional Logic with Filter
查看>>
Python基础学习
查看>>
document.all的用法详解
查看>>
chmod修改文件权限失败
查看>>
附魔位置详解
查看>>
POJ2096 Collecting Bugs(期望dp)
查看>>
cf519C. A and B and Team Training(找规律)
查看>>