创建自己struts2拦截器

步骤:


1:创建一个类实现com.opensymphony.xwork2.interceptor.Interceptor接口
2:实现intercept(ActionInvocation invocation) 方法
3:在struts.xml中配置拦截器
4: 链接到相应的action

文件目录:


struts2自身有很多的拦截器,在struts-core.jar中的struts-default.xml中。

首先创建一个HelloAction
package com.topwqp.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;
	}

}

然后创建相应拦截器:
init()方法是在启动服务器时加载拦截器时初始化的
destory()方法是在退出服务器调用的,但是这个方法不一定调用
ActionInvocation也是一个接口,对应Action或者其他的拦截器
解释:
An ActionInvocation represents the execution state of an Action. It holds the Interceptors and the Action instance. By repeated re-entrant execution of the invoke() method, initially by the ActionProxy, then by the Interceptors, the Interceptors are all executed, and then the Action and the Result.
关于invoke()方法是必须的:
invoke

String invoke()
              throws Exception
Invokes the next step in processing this ActionInvocation.
If there are more Interceptors, this will call the next one. If Interceptors choose not to short-circuit ActionInvocation processing and return their own return code, they will call invoke() to allow the next Interceptor to execute. If there are no more Interceptors to be applied, the Action is executed. If the ActionProxy.getExecuteResult() method returns true, the Result is also executed.

Returns:
the return code.
Throws:
Exception - can be thrown.



package com.topwqp.common.interceptor;

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

public class PrintMessageInterceptor implements Interceptor{

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("destory() method is invoked");
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		System.out.println("init() method is invoked");
		
	}

	//put interceptor code here
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("before invocation.invoked..........");
		String result = invocation.invoke();
		System.out.println(result);
		System.out.println("after  invocation.invoked..........");
		return  result;
	}
	

}

对应的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="printMessageInterceptor" 
    class="com.topwqp.common.interceptor.PrintMessageInterceptor">
    </interceptor>
 
         <interceptor-stack name="newStack">
         <interceptor-ref name="printMessageInterceptor"/>
         <interceptor-ref name="defaultStack"/>
         </interceptor-stack>
 
    </interceptors>
 
    <action name="helloAction" 
    class="com.topwqp.common.action.HelloAction" >
    <interceptor-ref name="newStack"/>
    <result name="success">pages/hello.jsp</result>
     </action>
 
   </package>
</struts>

对应的hello.jsp配置:
<html>
<body>
<h2>Hello  TOPWQP</h2>
</body>
</html>

启动访问: 路径:http://localhost:8080/Strut2OwnInterceptor/helloAction

结果:
五月 05, 2013 4:16:29 下午 org.apache.catalina.core.AprLifecycleListener init
INFO: Loaded APR based Apache Tomcat Native library 1.1.24.
五月 05, 2013 4:16:30 下午 org.apache.catalina.core.AprLifecycleListener init
INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
五月 05, 2013 4:16:32 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:Strut2OwnInterceptor' did not find a matching property.
五月 05, 2013 4:16:38 下午 org.apache.coyote.http11.Http11AprProtocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
五月 05, 2013 4:16:38 下午 org.apache.coyote.ajp.AjpAprProtocol init
INFO: Initializing Coyote AJP/1.3 on ajp-8009
五月 05, 2013 4:16:38 下午 org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 13193 ms
五月 05, 2013 4:16:38 下午 org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
五月 05, 2013 4:16:38 下午 org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.35
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Parsing configuration file [struts-default.xml]
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Unable to locate configuration files of the name struts-plugin.xml, skipping
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Parsing configuration file [struts-plugin.xml]
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Parsing configuration file [struts.xml]
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for class com.opensymphony.xwork2.ObjectFactory
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for class com.opensymphony.xwork2.conversion.impl.XWorkConverter
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.TextProvider
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.ActionProxyFactory
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.conversion.ObjectTypeDeterminer
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface org.apache.struts2.dispatcher.mapper.ActionMapper
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (jakarta) for interface org.apache.struts2.dispatcher.multipart.MultiPartRequest
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for class org.apache.struts2.views.freemarker.FreemarkerManager
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface org.apache.struts2.components.UrlRenderer
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.validator.ActionValidatorManager
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.util.ValueStackFactory
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.util.reflection.ReflectionProvider
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.util.reflection.ReflectionContextFactory
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.util.PatternMatcher
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface org.apache.struts2.dispatcher.StaticContentLoader
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.UnknownHandlerManager
init() method is invoked
五月 05, 2013 4:16:44 下午 org.apache.coyote.http11.Http11AprProtocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
五月 05, 2013 4:16:45 下午 org.apache.coyote.ajp.AjpAprProtocol start
INFO: Starting Coyote AJP/1.3 on ajp-8009
五月 05, 2013 4:16:45 下午 org.apache.catalina.startup.Catalina start
INFO: Server startup in 7060 ms
before invocation.invoked..........
HelloAction execute() is called
success
after  invocation.invoked..........





  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值