Struts 2 creating own interceptor
来源:互联网 发布:有淘宝就能贷款的软件 编辑:程序博客网 时间:2024/06/06 08:51
In this tutorial, it shows how to create an own interceptor in Struts 2.
Summary steps :
- Create a class implements
com.opensymphony.xwork2.interceptor.Interceptor
. - Implement the
intercept(ActionInvocation invocation)
method. - Configure the interceptor in the
struts.xml
. - Link it to action.
Struts 2 interceptors
Struts 2 comes with many ready interceptors, make sure you check the list of the available Struts 2 interceptors before you create your own interceptor.
A complete example to create an own interceptor :
1. Action
A simple action to forward the user request and print a message.
HelloAction.java
package com.mkyong.common.action;import com.opensymphony.xwork2.ActionSupport;public class HelloAction extends ActionSupport{ public String execute() throws Exception { System.out.println("HelloAction execute() is called"); return SUCCESS; }}
2. Interceptor
A full interceptor example.
PrintMsgInterceptor.java
package com.mkyong.common.interceptor;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.Interceptor;public class PrintMsgInterceptor implements Interceptor{ //called during interceptor destruction public void destroy() { System.out.println("CustomInterceptor destroy() is called..."); } //called during interceptor initialization public void init() { System.out.println("CustomInterceptor init() is called..."); } //put interceptor code here public String intercept(ActionInvocation invocation) throws Exception { System.out.println("CustomInterceptor, before invocation.invoke()..."); String result = invocation.invoke(); System.out.println("CustomInterceptor, after invocation.invoke()..."); return result; }}
Explanation
The interceptor class must implements the com.opensymphony.xwork2.interceptor.Interceptor
interface. During interceptor initialization, init()
is called; interceptor destruction, destroy()
is called. In last, put all interceptor code that does the work inside the intercept(ActionInvocation invocation)
method.
invocation.invoke()
In the interceptor intercept()
method, you must called the invocation.invoke()
and return it’s result. This is the method responsible for calling the next interceptor or the action. The action will failed to continue without calling the invocation.invoke()
method.
destroy()
is not reliable
It’s not recommend to put any code inside the destroy()
, because this method is not reliable. When your application server is force shutdown or be killed by command, the destroy()
will not be called.
3. struts.xml
Configure the interceptor in the struts.xml
file.
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <package name="default" namespace="/" extends="struts-default"> <interceptors> <interceptor name="printMsgInterceptor" class="com.mkyong.common.interceptor.PrintMsgInterceptor"></interceptor> <interceptor-stack name="newStack"> <interceptor-ref name="printMsgInterceptor"/> <interceptor-ref name="defaultStack" /> </interceptor-stack> </interceptors> <action name="helloAction" class="com.mkyong.common.action.HelloAction" > <interceptor-ref name="newStack"/> <result name="success">pages/hello.jsp</result> </action> </package></struts>
4. Demo
During the server initialization, the interceptor init()
method is called.
INFO: Overriding property struts.i18n.reload - old value: false new value: true15 Julai 2010 11:37:42 AM com.opensymphony.xwork2.util.logging.commons.CommonsLogger infoINFO: Overriding property struts.configuration.xml.reload - old value: false new value: trueCustomInterceptor init() is called...15 Julai 2010 11:37:42 AM org.apache.coyote.http11.Http11Protocol startINFO: Starting Coyote HTTP/1.1 on http-808015 Julai 2010 11:37:42 AM org.apache.jk.common.ChannelSocket initINFO: JK: ajp13 listening on /0.0.0.0:800915 Julai 2010 11:37:42 AM org.apache.jk.server.JkMain startINFO: Jk running ID=0 time=0/20 config=null15 Julai 2010 11:37:42 AM org.apache.catalina.startup.Catalina startINFO: Server startup in 994 ms
While you access the action via URL : http://localhost:8080/Struts2Example/helloAction.action
INFO: Overriding property struts.i18n.reload - old value: false new value: true15 Julai 2010 11:37:42 AM com.opensymphony.xwork2.util.logging.commons.CommonsLogger infoINFO: Overriding property struts.configuration.xml.reload - old value: false new value: trueCustomInterceptor init() is called...15 Julai 2010 11:37:42 AM org.apache.coyote.http11.Http11Protocol startINFO: Starting Coyote HTTP/1.1 on http-808015 Julai 2010 11:37:42 AM org.apache.jk.common.ChannelSocket initINFO: JK: ajp13 listening on /0.0.0.0:800915 Julai 2010 11:37:42 AM org.apache.jk.server.JkMain startINFO: Jk running ID=0 time=0/20 config=null15 Julai 2010 11:37:42 AM org.apache.catalina.startup.Catalina startINFO: Server startup in 994 msCustomInterceptor, before invocation.invoke()...HelloAction execute() is calledCustomInterceptor, after invocation.invoke()...
- Struts 2 creating own interceptor
- Struts 2 creating own interceptor
- Creating Your Own Procedures
- Creating Your Own Validation
- Struts 2 interceptor stack example
- Struts 2 execAndWait interceptor example
- Struts 2 interceptor stack example
- Struts 2 execAndWait interceptor example
- struts interceptor
- struts interceptor
- struts ----Interceptor
- struts interceptor
- struts interceptor
- struts --interceptor
- Creating your own JavaScript Library
- Creating Your Own Java Annotations
- Struts 2 override the interceptor parameters
- Struts 2 override the interceptor parameters
- STL源码——RB-Tree insert操作
- CoreData实践(三)——插入数据并使用SQLite Professional查看
- 打包静态库,文章mark
- c++引用与指针的区别(着重理解)
- 【Spring】Spring MVC Controller 参数中文乱码
- Struts 2 creating own interceptor
- cocos2dx+lua 3.5创建项目并移植到androdi
- gradle错误“Could not resolve all dependencies for configuration”
- android第三更(下载附件,通知栏显示进度)
- 复合类型与const关键字
- 智能指针用法
- DelegatingRequestProcessor作用
- iOS利用AFNetworking(AFN) 实现图片上传
- eclipse配置maven环境简记