`
lijunaccp
  • 浏览: 153571 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

struts2拦截器剖析

阅读更多
一.Struts2使用拦截器步骤:
1. 定义拦截器类
2. 声明拦截器
3. 使用拦截器
1. 定义拦截器MyInterceptor.java
package com.test.interceptor;

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

public class MyInterceptor implements Interceptor {

	@Override
	public void destroy() {
		System.out.println("destroy invoked");
	}

	@Override
	public void init() {
		System.out.println("init invoked");
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		System.out.println("intercept invoked");
		String result=invocation.invoke();
		System.out.println("intercept invoke result:"+result);
		return result;
	}

}

2. 声明拦截器 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="struts2" extends="struts-default">
	
		<interceptors>
			<interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor"></interceptor>
		</interceptors>
		
		<action name="helloworld" class="com.test.action.HelloWorld">
			<result>/helloworld.jsp</result>
		</action>
		
		<action name="login" class="com.test.action.LoginAction">
			<result>/result.jsp</result>
			<result name="input">/login2.jsp</result>
		</action>
		
		<action name="converterAction" class="com.test.action.PointAction" method="test" >
			<result name="success">/output.jsp</result>
			<result name="input">/error.jsp</result>
		</action>
		
		<action name="register" class="com.test.action.RegisterAction">
			<result name="success">/success.jsp</result>
			<result name="input">/register.jsp</result>
		</action>
		
	</package>
</struts>

3. 使用拦截器 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="struts2" extends="struts-default">
	
		<interceptors>
			<interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor"></interceptor>
		</interceptors>
		
		<action name="helloworld" class="com.test.action.HelloWorld">
			<result>/helloworld.jsp</result>
		</action>
		
		<action name="login" class="com.test.action.LoginAction">
			<result>/result.jsp</result>
			<result name="input">/login2.jsp</result>
		</action>
		
		<action name="converterAction" class="com.test.action.PointAction" method="test" >
			<result name="success">/output.jsp</result>
			<result name="input">/error.jsp</result>
		</action>
		
		<action name="register" class="com.test.action.RegisterAction">
			<result name="success">/success.jsp</result>
			<result name="input">/register.jsp</result>
			
			<interceptor-ref name="myInterceptor"></interceptor-ref>
			
		</action>
		
	</package>
</struts>

注:拦截器在服务器启动时就加载,并初始化。
二.Struts2.xml中使用拦截器问题
1. 如上使用拦截器会发现输入结果不能被传到结果页面
这是因为strut2.xml继承了struts-default文件,这个XML文件中定义了很多拦截器,且定义了默认使用的拦截器组defaultStack,如果在struts.xml中Aciton使用了自定的拦截器,默认的使用的拦截器无效,解决方法就是在引用一下defaultStack拦截器
<action name="register" class="com.test.action.RegisterAction">
			<result name="success">/success.jsp</result>
			<result name="input">/register.jsp</result>
			
			<interceptor-ref name="myInterceptor"></interceptor-ref>
			<interceptor-ref name="defaultStack"></interceptor-ref>
		</action>

三.Struts2.xml中使用拦截器同Filter可以对黑名单进行操作
1. 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="struts2" extends="struts-default">
	
		<interceptors>
			<interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor">
				<param name="hello">world</param>
			</interceptor>
		</interceptors>
		
		<action name="helloworld" class="com.test.action.HelloWorld">
			<result>/helloworld.jsp</result>
		</action>
		
		<action name="login" class="com.test.action.LoginAction">
			<result>/result.jsp</result>
			<result name="input">/login2.jsp</result>
		</action>
		
		<action name="converterAction" class="com.test.action.PointAction" method="test" >
			<result name="success">/output.jsp</result>
			<result name="input">/error.jsp</result>
		</action>
		
		<action name="register" class="com.test.action.RegisterAction">
			<result name="success">/success.jsp</result>
			<result name="input">/register.jsp</result>
			
			<interceptor-ref name="myInterceptor"></interceptor-ref>
			<interceptor-ref name="defaultStack"></interceptor-ref>
		</action>
		
	</package>
</struts>

2.  MyInterceptor.java
package com.test.interceptor;

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

public class MyInterceptor implements Interceptor {
	
	private String hello;
	
	public String getHello() {
		return hello;
	}

	public void setHello(String hello) {
		this.hello = hello;
	}

	@Override
	public void destroy() {
		System.out.println("destroy invoked");
	}

	@Override
	public void init() {
		System.out.println("init invoked");
		System.out.println("hello:"+hello);
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		System.out.println("intercept invoked");
		String result=invocation.invoke();
		System.out.println("intercept invoke result:"+result);
		return result;
	}

}

四.为了避免实现Interceptor接口时要实现init(),destory(),只需要interceptor(),可以继承抽象类AbstractInterceptor
1. AbstractInterceptor.java
package com.test.interceptor;

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

public class MyInterceptor2 extends AbstractInterceptor {

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		System.err.println("MyInterceptor2 invoked");
		String result=invocation.invoke();
		System.out.println("MyInterceptor2 invoke result:"+result);
		return result;
	}

}

2. 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="struts2" extends="struts-default">
	
		<interceptors>
			<interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor">
				<param name="hello">world</param>
			</interceptor>
			<interceptor name="myInterceptor2" class="com.test.interceptor.MyInterceptor2">
			</interceptor>
		</interceptors>
		
		<action name="helloworld" class="com.test.action.HelloWorld">
			<result>/helloworld.jsp</result>
		</action>
		
		<action name="login" class="com.test.action.LoginAction">
			<result>/result.jsp</result>
			<result name="input">/login2.jsp</result>
		</action>
		
		<action name="converterAction" class="com.test.action.PointAction" method="test" >
			<result name="success">/output.jsp</result>
			<result name="input">/error.jsp</result>
		</action>
		
		<action name="register" class="com.test.action.RegisterAction">
			<result name="success">/success.jsp</result>
			<result name="input">/register.jsp</result>
			
			<interceptor-ref name="myInterceptor"></interceptor-ref>
			<interceptor-ref name="myInterceptor2"></interceptor-ref>
			<interceptor-ref name="defaultStack"></interceptor-ref>
		</action>
		
	</package>
</struts>

五.Action中的方法拦截器,继承抽象类MethodFilterInterceptor
1.  MethodFilterInterceptor.java
package com.test.interceptor;

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

public class MyInterceptor3 extends MethodFilterInterceptor {

	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		System.out.println("MyInterceptor3 invoked");
		String result=invocation.invoke();
		System.out.println("MyInterceptor3 invoke result:"+result);
		return result;
	}

}

2. 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="struts2" extends="struts-default">
	
		<interceptors>
			<interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor">
				<param name="hello">world</param>
			</interceptor>
			<interceptor name="myInterceptor2" class="com.test.interceptor.MyInterceptor2">
			</interceptor>
			<interceptor name="myInterceptor3" class="com.test.interceptor.MyInterceptor3">
			</interceptor>
		</interceptors>
		
		<action name="helloworld" class="com.test.action.HelloWorld">
			<result>/helloworld.jsp</result>
		</action>
		
		<action name="login" class="com.test.action.LoginAction">
			<result>/result.jsp</result>
			<result name="input">/login2.jsp</result>
		</action>
		
		<action name="converterAction" class="com.test.action.PointAction" method="test" >
			<result name="success">/output.jsp</result>
			<result name="input">/error.jsp</result>
		</action>
		
		<action name="register" class="com.test.action.RegisterAction">
			<result name="success">/success.jsp</result>
			<result name="input">/register.jsp</result>
			
			<interceptor-ref name="myInterceptor"></interceptor-ref>
			<interceptor-ref name="myInterceptor2"></interceptor-ref>
			<interceptor-ref name="myInterceptor3">
				<!-- <param name="excludeMethods">execute</param> --> 
没有param就拦截所有方法,includeMethods拦截指定方法,excludeMethods不拦截指定方法
			</interceptor-ref>
			<interceptor-ref name="defaultStack"></interceptor-ref>
		</action>
		
	</package>
</struts>

六.拦截器栈(拦截器的集合)
1. 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="struts2" extends="struts-default">
	
		<interceptors>
			<interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor">
				<param name="hello">world</param>
			</interceptor>
			<interceptor name="myInterceptor2" class="com.test.interceptor.MyInterceptor2">
			</interceptor>
			<interceptor name="myInterceptor3" class="com.test.interceptor.MyInterceptor3">
			</interceptor>
			
			<interceptor-stack name="myStack">
				<interceptor-ref name="myInterceptor"></interceptor-ref>
				<interceptor-ref name="myInterceptor2"></interceptor-ref>
			</interceptor-stack>
		</interceptors>
		
		<action name="helloworld" class="com.test.action.HelloWorld">
			<result>/helloworld.jsp</result>
		</action>
		
		<action name="login" class="com.test.action.LoginAction">
			<result>/result.jsp</result>
			<result name="input">/login2.jsp</result>
		</action>
		
		<action name="converterAction" class="com.test.action.PointAction" method="test" >
			<result name="success">/output.jsp</result>
			<result name="input">/error.jsp</result>
		</action>
		
		<action name="register" class="com.test.action.RegisterAction">
			<result name="success">/success.jsp</result>
			<result name="input">/register.jsp</result>
			<!-- 
			<interceptor-ref name="myInterceptor"></interceptor-ref>
			<interceptor-ref name="myInterceptor2"></interceptor-ref>
			 -->
			 
			 <interceptor-ref name="myStack"></interceptor-ref>
			 
			<interceptor-ref name="defaultStack"></interceptor-ref>
		</action>
		
	</package>
</struts>

七.自定义默认的拦截器
1. 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="struts2" extends="struts-default">
	
		<interceptors>
			<interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor">
				<param name="hello">world</param>
			</interceptor>
			<interceptor name="myInterceptor2" class="com.test.interceptor.MyInterceptor2">
			</interceptor>
			<interceptor name="myInterceptor3" class="com.test.interceptor.MyInterceptor3">
			</interceptor>
			
			<interceptor-stack name="myStack">
				<interceptor-ref name="myInterceptor"></interceptor-ref>
				<interceptor-ref name="myInterceptor2"></interceptor-ref>
				<interceptor-ref name="defaultStack"></interceptor-ref>
			</interceptor-stack>
		</interceptors>
		
		<default-interceptor-ref name="myStack"></default-interceptor-ref>
		
		<action name="helloworld" class="com.test.action.HelloWorld">
			<result>/helloworld.jsp</result>
		</action>
		
		<action name="login" class="com.test.action.LoginAction">
			<result>/result.jsp</result>
			<result name="input">/login2.jsp</result>
		</action>
		
		<action name="converterAction" class="com.test.action.PointAction" method="test" >
			<result name="success">/output.jsp</result>
			<result name="input">/error.jsp</result>
		</action>
		
		<action name="register" class="com.test.action.RegisterAction">
			<result name="success">/success.jsp</result>
			<result name="input">/register.jsp</result>
			<!-- 
			<interceptor-ref name="myInterceptor"></interceptor-ref>
			<interceptor-ref name="myInterceptor2"></interceptor-ref>
			 <interceptor-ref name="myStack"></interceptor-ref>
			 -->
			
		</action>
		
	</package>
</struts>

注:自定义默认的拦截器会对所有Action生效,所有一般情况不会用自定默认拦截器
八.在拦截器中增加监听器(观察者模式)
1.  定义监听器MyListener.java实现PreResultListener(在execute方法执行完,result执行之前调用)接口
package com.test.listener;

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

public class MyListener implements PreResultListener {

	@Override
	public void beforeResult(ActionInvocation invocation, String resultCode) {
		System.out.println("MyListener invoked");
	}

}

2. 在拦截器MyInterceptor中增加监听器
package com.test.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.test.listener.MyListener;

public class MyInterceptor implements Interceptor {
	
	private String hello;
	
	public String getHello() {
		return hello;
	}

	public void setHello(String hello) {
		this.hello = hello;
	}

	@Override
	public void destroy() {
		System.out.println("destroy invoked");
	}

	@Override
	public void init() {
		System.out.println("init invoked");
		System.out.println("hello:"+hello);
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		
		invocation.addPreResultListener(new MyListener());
		
		System.out.println("intercept invoked");
		String result=invocation.invoke();
		System.out.println("intercept invoke result:"+result);
		return result;
	}

}

对于以上方式,只有一处会用到的一个类的一个简单方法,可以不用自定义监听器,直接在拦截器中用一个匿名类来实现,如下:
@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		
		invocation.addPreResultListener(new PreResultListener(){
			@Override
			public void beforeResult(ActionInvocation invocation,
					String resultCode) {
				System.out.println("hello world");
			}
		});
		
		System.out.println("intercept invoked");
		String result=invocation.invoke();
		System.out.println("intercept invoke result:"+result);
		return result;
	}

九.拦截器应用的场合(最重要的应用是权限验证)
1. 定义权限拦截器AuthInterceptor.java
package com.test.interceptor;

import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class AuthInterceptor extends AbstractInterceptor {

	@SuppressWarnings("unchecked")
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		Map map=invocation.getInvocationContext().getSession();
		if(null==map.get("user")){   //用户没有登录
			return Action.LOGIN;
		}else{
			return invocation.invoke();  //转到下一个拦截器
		}
	}

}

2. 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="struts2" extends="struts-default">
	
		<interceptors>
			<interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor">
				<param name="hello">world</param>
			</interceptor>
			<interceptor name="myInterceptor2" class="com.test.interceptor.MyInterceptor2">
			</interceptor>
			<interceptor name="myInterceptor3" class="com.test.interceptor.MyInterceptor3">
			</interceptor>
//声明权限拦截器
			<interceptor name="authInterceptor" class="com.test.interceptor.AuthInterceptor">
			</interceptor>
//使用权限拦截器		
			<interceptor-stack name="myStack">
				<interceptor-ref name="authInterceptor"></interceptor-ref>
				<interceptor-ref name="myInterceptor"></interceptor-ref>
				<interceptor-ref name="myInterceptor2"></interceptor-ref>
				<interceptor-ref name="defaultStack"></interceptor-ref>
			</interceptor-stack>
		</interceptors>
//使用全局结果集,对所有Action生效	
		<global-results>     
//type的值,可以在struts-default.xml中查找,redirect是重定向,默认是dispatcher转发                 
			<result name="login" type="redirect">/login.jsp</result>
		</global-results>
		<!-- 
		<default-interceptor-ref name="myStack"></default-interceptor-ref>
		 -->
		<action name="helloworld" class="com.test.action.HelloWorld">
			<result>/helloworld.jsp</result>
		</action>
		
		<action name="login" class="com.test.action.LoginAction">
			<result>/result.jsp</result>
			<result name="input">/login2.jsp</result>
		</action>
		
		<action name="converterAction" class="com.test.action.PointAction" method="test" >
			<result name="success">/output.jsp</result>
			<result name="input">/error.jsp</result>
		</action>
		
		<action name="register" class="com.test.action.RegisterAction">
			<result name="success">/success.jsp</result>
			<result name="input">/register.jsp</result>
			<!-- 
			<interceptor-ref name="myInterceptor"></interceptor-ref>
			<interceptor-ref name="myInterceptor2"></interceptor-ref>
			 -->
			 <interceptor-ref name="myStack"></interceptor-ref>
			
			
		</action>
		
	</package>
</struts>


分享到:
评论

相关推荐

    Struts2拦截器原理分析

    Struts2拦截器原理分析实例 博文链接:https://zmx.iteye.com/blog/457432

    struts2拦截器

    最全的InterCepter的基础,面向对象AOP的透彻分析

    Struts2之拦截器原理分析及使用-上案例struts007

    参考博文:http://blog.csdn.net/u011638419/article/details/41510483

    Struts2之拦截器原理分析及使用案例struts008

    参考博文:http://blog.csdn.net/u011638419/article/details/41592837

    struts2过滤器和拦截器的区别分析

    主要介绍了struts2过滤器和拦截器的区别,简单分析了struts2框架中过滤器和拦截器的概念与相关使用区别,需要的朋友可以参考下

    IBM Struts2培训PPT.7z

    IBMStruts2培训PPT (大全集) 主讲:刘雷 完整实用 第一章:认识体验Struts2.ppt 第二章:struts2架构剖析.ppt 第三章:struts2配置详解.ppt ...第九章:struts2拦截器.ppt + Struts2开发详解.ppt

    JavaWeb中Struts2拦截器深入分析(一)

    主要为大家详细介绍了JavaWeb中Struts2拦截器的功能,感兴趣的小伙伴们可以参考一下

    Struts拦截器

    Struts拦截器Demo,适合理解和分析拦截器的用法。

    Struts2架构剖析

    action command在穿越了一系列Struts2和XWork的拦截器之后,最后执行用户编写的Action类,在Action执行之后,响应还会穿越相同的拦截器(按照与请求相反的顺序),最后通过Struts2的HttpServletResponse转换成Web可...

    Struts2面试题分析_尚硅谷_佟刚_.pdf

    Struts2面试题分析_尚硅谷_佟刚 1. 简述 Struts2 的工作流程 2. Struts2 拦截器 和 过滤器 的区别 3. 为什么要使用 Struts2 & Struts2 的优点.....

    struts2 ppt 教程 十章

    第一章:认识体验Struts 2 第二章:struts2架构剖析 第三章:struts2配置详解 第四章:struts2中的OGNL 第五章:struts2标签库 第六章:struts2国际化 ...第九章:struts2拦截器 第十章:struts2类型转换

    深入浅出Struts2(附源码)

    作者处处从实战出发,在丰富的示例中直观地探讨了许多实用的技术,如数据类型转换、文件上传和下载、提高Struts 2应用的安全性、调试与性能分析、FreeMarker、Velocity、Ajax,等等。跟随作者一道深入Struts 2,聆听...

    struts2漏洞分析及解决方案

    对struts2漏洞的分析,及解决方法,主要通过自定义一个拦截器来完成,简单有效

    Struts2源码分析之ParametersInterceptor拦截器

    主要介绍了Struts2源码分析之ParametersInterceptor拦截器,ParametersInterceptor拦截器其主要功能是把ActionContext中的请求参数设置到ValueStack中,,需要的朋友可以参考下

    拦截器应用

    主要对于对struts2高级框架感兴趣的,可以当参考文档使用,主要对拦截器详细分析

    深入浅出Struts2

    第10章 Model Driven和Preparable拦截器 第11章 持久层 第12章 文件的上传 第13章 文件的下载 第14章 提高Struts应用程序的安全性 第15章 防止重复提交 第16章 调试与性能分析 第17章 进度条 第18章 定制拦截器 第19...

    详解Struts2中对未登录jsp页面实现拦截功能

    主要介绍了Struts2中对未登录jsp页面进行拦截功能的实现,在演示源码的同时对步骤和原理进行了分析,具有一定参考价值,需要得朋友可以了解下。

    尚硅谷Java视频_Struts2视频教程

    每个技术点都配备案例和代码,对于拦截器、Struts2 运行流程分析、值栈等技术点的讲授更是深入解析源代码,授之以渔。  学习完本Java视频教程,你会真正理解 Struts2 的优雅和简洁,并使你具备开发 Struts2 企业...

Global site tag (gtag.js) - Google Analytics