您的位置:首页 > 编程语言 > Java开发

Struts2自定义拦截器

2016-11-21 15:22 337 查看

Struts2 拦截器举例



Struts2 拦截器

Struts2 拦截器在访问某个 Action 方法之前或之后实施拦截,Struts2 拦截器是可插拔的,拦截器是 AOP(spring会讲到,现在不理会) 的一种实现.

拦截器栈(Interceptor Stack): 将拦截器按一定的顺序联结成一条链. 在访问被拦截的方法时,Struts2拦截器链中的拦截器就会按其之前定义的顺序被依次调用.

Interceptor 接口

每个拦截器都是实现了 com.opensymphony.xwork2.interceptor.Interceptor接口的 Java 类:



init: 该方法将在拦截器被创建后立即被调用, 它在拦截器的生命周期内只被调用一次. 可以在该方法中对相关资源进行必要的初始化

interecept: 每拦截一个动作请求, 该方法就会被调用一次. 相当于doFilter方法.

destroy: 该方法将在拦截器被销毁之前被调用, 它在拦截器的生命周期内也只被调用一次.

Struts 会依次调用程序员为某个 Action 而注册的每一个拦截器的 interecept 方法.

每次调用 interecept 方法时, Struts 会传递一个 ActionInvocation 接口的实例.

ActionInvocation: 代表一个给定动作的执行状态, 拦截器可以从该类的对象里获得与该动作相关联的 Action 对象和

Result 对象. 在完成拦截器自己的任务之后, 拦截器将调用 ActionInvocation 对象的 invoke 方法前进到

Action 处理流程的下一个环节.

还可以调用 ActionInvocation 对象的 addPreResultListener 方法给 ActionInvocation对象 “挂” 上一个或多个 PreResultListener 监听器该监听器对象可以在动作执行完毕之后, 开始执行动作结果之前做些事情

AbstractInterceptor 类实现了 Interceptor 接口. 并为 init, destroy

提供了一个空白的实现

自定义拦截器

定义自定义拦截器的步骤

自定义拦截器

在 struts.xml 文件中配置自定义的拦截器

要自定义拦截器需要实现com.opensymphony.xwork2.interceptor.Interceptor接口:

package cn.itcast.aop;

import java.util.Map;

import org.apache.struts2.ServletActionContext;

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

@SuppressWarnings("serial")
public class ExpessionInterceptor implements Interceptor {

public void init() {
System.out.println("ExpessionInterceptor ********* init()");

}

public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("ExpessionInterceptor ********* intercept()");

//cn.itcast.aop.UserAction@15b5783,动作类的对象
System.out.println("invocation.getAction() : "+invocation.getAction());

//cn.itcast.aop.UserAction@15b5783,与invocation.getAction()方法获取的是同一的对象
System.out.println("invocation.getProxy().getAction() : "+invocation.getProxy().getAction());

//userAction_save,自定义配置文件中的action标签的name属性的值
System.out.println("invocation.getProxy().getActionName() : "+invocation.getProxy().getActionName());

//save,对应动作类指定要执行的方法名
System.out.println("invocation.getProxy().getMethod() : "+invocation.getProxy().getMethod());

//  /aop,自定义配置文件中的package标签的namespace属性的值
System.out.println("invocation.getProxy().getNamespace() : "+invocation.getProxy().getNamespace());

//null  返回结果
System.out.println("invocation.getResult() : "+invocation.getResult());

Map sessionMap = ServletActionContext.getContext().getSession();

Object obj = sessionMap.get("user");

if(obj==null||obj.equals("")){
return "error";
}else{
return "success";
}
}

public void destroy() {
System.out.println("ExpessionInterceptor ********* destroy()");

}

}


在 struts_aop.xml 文件中配置自定义的拦截器

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="aop" namespace="/aop" extends="struts-default">

<interceptors>
<!-- 声明自定义的拦截器 -->
<interceptor name="expessionInterceptor" class="cn.itcast.aop.ExpessionInterceptor" />

<!-- 声明自定义拦截器栈 -->
<interceptor-stack name="expessionStack">
<interceptor-ref name="defaultStack"/>

<!-- 配置使用自定义拦截器 -->
<interceptor-ref name="expessionInterceptor"/>

</interceptor-stack>
</interceptors>

<!-- 配置修改struts2框架运行时,默认执行的是自定义拦截器栈 -->
<default-interceptor-ref name="expessionStack" />

<action name="userAction_save" class="cn.itcast.aop.UserAction" method="save">
<result name="success">/aop/success.jsp</result>
<result name="error">/aop/error.jsp</result>
</action>
</package>
</struts>


因为struts2中如文件上传,数据验证,封装请求参数到action等

功能都是由系统默认的defaultStack中的拦截器实现的,所以

我们定义的拦截器需要引用系统默认的defaultStack,这样应用才

可以使用struts2框架提供的众多功能。

如果希望包下的所有action都使用自定义的拦截器,

可以通过
<default-interceptor-ref name=“permissionStack”/>


把拦截器定义为默认拦截器。注意:每个包只能指定一个默认拦截器。

另外,一旦我们为该包中的某个action显式指定了某个拦截器,

则默认拦截器不会起作用。

struts.xml文件中引入自定义配置文件

<include file="cn/itcast/aop/struts_aop.xml"></include>


jap页面:

index.jsp

<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/struts-tags"   prefix="s"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
用户登录!!!

<%
session.setAttribute("user","user");
%>

</body>
</html>


test.jsp

<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/struts-tags"   prefix="s"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
测试拦截器:<br>
<a href="${pageContext.request.contextPath}/aop/userAction_save.action">test</a><br>

</body>
</html>


error.jsp

<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/struts-tags"   prefix="s"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
对不起,您没有权限<br>
</body>
</html>


success.jsp

<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/struts-tags"   prefix="s"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
成功!!!!<br> ${id}
</body>
</html>


测试:

为登陆直接进入test.jsp



点击超链接



进入index.jsp进行登陆先



在进入test.jsp



点击超链接



后台的输出:



Struts2 自带的拦截器(1)



Struts2 自带的拦截器(2)

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  struts2.0