在本教程中,它显示了如何在Struts 2中创建自己的拦截器。
摘要步骤:
- 创建一个实现com.opensymphony.xwork2.interceptor.Interceptor的类。
- 实现intercept(ActionInvocation调用)方法。
- 在struts.xml中配置拦截器。
- 将其链接到动作。
Struts 2拦截器
Struts 2附带了许多现成的拦截器,请确保在创建自己的拦截器之前检查可用的Struts 2拦截器的列表。
创建自己的拦截器的完整示例:
1.行动
转发用户请求并打印消息的简单操作。
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.拦截器
完整的拦截器示例。
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;
}
}
说明
拦截器类必须实现com.opensymphony.xwork2.interceptor.Interceptor接口 。 在拦截器初始化期间,将调用init() 。 拦截器破坏,称为destroy() 。 最后,将完成该工作的所有拦截器代码放入intercept(ActionInvocation invocation)方法中。
invocation.invoke()
在拦截器intercept()方法中, 必须调用invocation.invoke()并返回其结果。 这是负责调用下一个拦截器或操作的方法。 如果不调用invocation.invoke()方法,该操作将无法继续。
在拦截器intercept()方法中, 必须调用invocation.invoke()并返回其结果。 这是负责调用下一个拦截器或操作的方法。 如果不调用invocation.invoke()方法,该操作将无法继续。
destroy()不可靠
不建议将任何代码放入destroy()内 ,因为此方法不可靠。 当您的应用程序服务器被强制关闭或被命令杀死时, destroy()将不会被调用。
不建议将任何代码放入destroy()内 ,因为此方法不可靠。 当您的应用程序服务器被强制关闭或被命令杀死时, destroy()将不会被调用。
3. struts.xml
在struts.xml文件中配置拦截器。
<?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.演示
在服务器初始化期间,将调用拦截器init()方法。
INFO: Overriding property struts.i18n.reload - old value: false new value: true
15 Julai 2010 11:37:42 AM com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
INFO: Overriding property struts.configuration.xml.reload - old value: false new value: true
CustomInterceptor init() is called...
15 Julai 2010 11:37:42 AM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
15 Julai 2010 11:37:42 AM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
15 Julai 2010 11:37:42 AM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/20 config=null
15 Julai 2010 11:37:42 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 994 ms
通过URL访问操作时: http:// localhost:8080 / Struts2Example / helloAction.action
INFO: Overriding property struts.i18n.reload - old value: false new value: true
15 Julai 2010 11:37:42 AM com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
INFO: Overriding property struts.configuration.xml.reload - old value: false new value: true
CustomInterceptor init() is called...
15 Julai 2010 11:37:42 AM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
15 Julai 2010 11:37:42 AM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
15 Julai 2010 11:37:42 AM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/20 config=null
15 Julai 2010 11:37:42 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 994 ms
CustomInterceptor, before invocation.invoke()...
HelloAction execute() is called
CustomInterceptor, after invocation.invoke()...
参考
翻译自: https://mkyong.com/struts2/struts-2-creating-own-interceptor/