12.自定义拦截器
       拦截器是Struts2的一个重要特性。因为Struts2的大多数核心功能都是通过拦截器实现的。
拦截器之所以称之为“拦截器”,是因为它可以拦截Action方法的执行,
即在Acton方法执行之前或之后执行,以加强Action方法的功能。
例如,一般情况下,用户在打开某个页面之前,需要先登录,否则是无法对资源进行访问的。这就是权限拦截器。
1、定义拦截器类
自定义的拦截器类需要实现拦截器接口com.opensymphony.xwork2.interceptor.Interceptor。
该接口中定义了三个方法:
public void init():拦截器实例被创建之前被调用
public void destroy():拦截器实例被销毁之前被调用
public String intercept (ActionInvocation invocation) throws Exception

该方法在Action执行之前被调用,拦截器的附加功能在该方法中实现。

执行参数invocation的invoke()方法,就是调用Action方法在执行。

intercept()方法的完全结构如下图所示:
 
2、注册拦截器
拦截器类在定义好后,需要在struts.xml配置文件中注册,以通知Struts2框架。
在<package>中添加如下标签:
<package name="one" extends="struts-default">
<interceptors>
<interceptor name="permission" class="interceptors.PermissionInterceptor"/>
</interceptors>
<action name="my" class="actions.MyAction">
<result>/message.jsp</result>
</action>
</package>

注册后的拦截器,便可由Action通过配置文件配置来使用了。

<package name="one" extends="struts-default">
<interceptors>
<interceptor name="permission" class="interceptors.PermissionInterceptor"/>
</interceptors>
<action name="my" class="actions.MyAction">
<interceptor-ref name="permission" />
</action>
</package>

但,这种用法非常不好。因为这样会使该Action不能再使用Struts2自带的各种核心拦截器,而只使用这一个了。

若要使该Action既能使用自定义的拦截器,又能使用Struts2自带的拦截器,那么就需要在包中定义拦截器栈:

在<interceptors>中添加<interceptor-stack>标签。
<interceptors>
<interceptor name="permission"
class="interceptors.PermissionInterceptor"/>
<interceptor-stack name="permissionStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="permission" />
</interceptor-stack>
</interceptors>
defaultStack为Struts2自带的各种拦截器的栈。
即该自定义的拦截器栈permissionStack中,包括一个拦截器栈与一个拦截器。
<action name="my" class="actions.MyAction">
<interceptor-ref name=" permissionStack " />
</action>

当然,不定义permissionStack 拦截器栈直接按如下写法也可:

<action name="list" class="actions.MyAction">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="permission" />
</action>

这样,该action即可以使用defaultStack核心拦截器栈,又可以使用自定义的拦截器。

若该<package>中的所有Action均要使用该拦截器,
一种方式是,每个Action均像上面那样指定要使用的拦截器栈permissionStack ;
另一种方式是,指定默认的拦截器栈。
即在<package>下定义:
<default-interceptor-ref name=" permissionStack " /> 
这样,每一个本包中的Action不用声明使可直接使用该拦截器栈。
注意:
    每个包只能指定一个默认拦截器。
    另外,一旦为该包中的某个action显式指定了某个拦截器,则默认拦截器就不会起作用了。
实例:权限拦截器--interceptor
只有经过登录的用户方可访问Action中的方法,否则,将返回“无权访问”提示。
本例的登录,由一个JSP页面完成。即在该页面里将用户信息放入session中。
也就是说,只要访问过该页面,就说明登录了。没访问过,则为未登录用户。
代码文档目录:
Step1:编写index.jsp与login.jsp页面
index.jsp源码如下:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
This is my JSP page. <br>
</body>
</html>

login.jsp源码如下:

<%@page pageEncoding="utf-8"%>
<%
session.setAttribute("user", "aynu");
%>
登陆成功!

Step2:编写MyAction.java与PermissionInterceptor.java

MyAction.java源码如下:
package actions;

public class MyAction {

  public String execute(){
System.out.println("执行Action方法");
return "success";
}
}

PermissionInterceptor.java源码如下:

package interceptors;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor; public class PermissionInterceptor implements Interceptor { public void destroy() { } public void init() { } public String intercept(ActionInvocation invocation) throws Exception {
String user=(String) ActionContext.getContext().getSession().get("user"); //执行Action方法
String result="success";
if (user!=null) {
System.out.println("Action方法执行之前的附加功能");
result = invocation.invoke();
System.out.println("Action方法执行之后的附加功能");
ActionContext.getContext().getSession().put("message","欢迎访问");
}else{
ActionContext.getContext().getSession().put("message","无权访问"); }
return result;
} }
Step3:编写web.xml与struts.xml
web.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

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="one" extends="struts-default">
<interceptors>
<interceptor name="permission" class="interceptors.PermissionInterceptor"/>
<interceptor-stack name="permissionStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="permission"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="permissionStack"/>
<action name="my" class="actions.MyAction">
<result>/message.jsp</result>
</action> </package> </struts>

Step4:编写message.jsp页面

<%@ page pageEncoding="utf-8" isELIgnored="false"%>

<html>
<head>
<title>message page</title>
</head>
<body>
提示信息:${message}
</body>
</html>

部署发布,启动tomcat,输入地址:

http://127.0.0.1:8080/interceptor_test/my.action
 

 输入地址:
 http://127.0.0.1:8080/interceptor_test/login.jsp


输入地址:

http://127.0.0.1:8080/interceptor_test/my.action
 

 
 
 
 
 
 
 
 
 
 
 
 
 

12.Struts2自定义拦截器的更多相关文章

  1. Struts2自定义拦截器Interceptor以及拦截器登录实例

    1.在Struts2自定义拦截器有三种方式: -->实现Interceptor接口 public class QLInterceptorAction implements Interceptor ...

  2. struts2自定义拦截器 设置session并跳转

    实例功能:当用户登陆后,session超时后则返回到登陆页面重新登陆. 为了更好的实现此功能我们先将session失效时间设置的小点,这里我们设置成1分钟 修改web.xml view plainco ...

  3. 【Java EE 学习 35 下】【struts2】【struts2文件上传】【struts2自定义拦截器】【struts2手动验证】

    一.struts2文件上传 1.上传文件的时候要求必须使得表单的enctype属性设置为multipart/form-data,把它的method属性设置为post 2.上传单个文件的时候需要在Act ...

  4. Struts2 自定义拦截器

    自定义拦截器(权限管理),包含了对ajax和表单请求的拦截 package com.interceptor; import java.io.IOException; import java.io.Pr ...

  5. Struts2自定义拦截器

    1. 需求 自定义拦截器实现,用户登录的访问控制. 2. 定义拦截器类 public class LoginInterceptor extends AbstractInterceptor { @Ove ...

  6. struts2自定义拦截器与cookie整合实现用户免重复登入

    目的:测试开发时,为了减少用户登入这个繁琐的登入验证,就用struts2做了个简单的struts2拦截器,涉及到了与cookie整合,具体的看代码 结构(两部份)=struts2.xml+自定义拦截器 ...

  7. 5、Struts2自定义拦截器

    一.拦截器相关知识 1.Struts2框架剖析 Holly版本生活案例: 影视公司(拍电影)    ActionMapper 传媒公司(包装明星) ActionMapping 明星           ...

  8. Struts2自定义拦截器处理全局异常

    今天在整理之前的项目的时候想着有的action层没有做异常处理,于是想着自定义拦截器处理一下未拦截的异常. 代码: package cn.xm.exam.action.safeHat; import ...

  9. Struts2自定义拦截器——完整实例代码

    比如一个网上论坛过滤系统,将网友发表的不文明.不和谐的语言,通过拦截器对这些文字进行自动替代. 该项目包含: 1.自定义拦截器(MyInterceptor.java) 2.发表评论的页面(news.j ...

随机推荐

  1. touchstart、touchmove、touchend 实现移动端上的触屏拖拽

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. Javascript外部对象

    Window 浏览器: - location:地址 - history:历史 - Document:文档 - screen:窗口 - navigator:帮助 > 1.外部对象就是浏览器提供的A ...

  3. WPF整理-Style

    "Consistency in a user interface is an important trait; there are many facets of consistency,   ...

  4. CSS篇之DIV+CSS布局

    <div></div> div与其他标签一样,也是一个XHTML所支持的标签. div是XHTML中指定的,远门用于布局设计的容器标记. 简单的CSS布局 头部 内容 页脚 & ...

  5. poj1200-Crazy Search(hash入门经典)

    Hash:一般是一个整数.就是说通过某种算法,可以把一个字符串"压缩" 成一个整数.一,题意: 给出两个数n,nc,并给出一个由nc种字符组成的字符串.求这个字符串中长度为n的不同 ...

  6. Second Day: 关于Button监听事件的三种方法(匿名类、外部类、继承接口)

    第一种:通过匿名类实现对Button事件的监听 首先在XML文件中拖入一个Button按钮,并设好ID,其次在主文件.java中进行控件初始化(Private声明),随后通过SetOnClickLis ...

  7. [搜索引擎]Sphinx的介绍和原理探索

    What/Sphinx是什么 定义 Sphinx是一个全文检索引擎. 特性 索引和性能优异 易于集成SQL和XML数据源,并可使用SphinxAPI.SphinxQL或者SphinxSE搜索接口 易于 ...

  8. ENode框架Conference案例分析系列之 - 上下文划分和领域建模

    前面一片文章,我介绍了Conference案例的核心业务,为了方便后面的分析,我这里再列一下: 业务描述 Conference是这样一个系统,它提供了一个在线创建会议以及预订会议座位的平台.这个系统的 ...

  9. Android知识杂记(四)

    1.完整退出activity的设计思路 1.1 封装一个基础activity类 public abstract class RootActivity extends FragmentActivity{ ...

  10. 玩转Windows服务系列——Windows服务小技巧

    伴随着研究Windows服务,逐渐掌握了一些小技巧,现在与大家分享一下. 将Windows服务转变为控制台程序 由于默认的Windows服务程序,编译后为Win32的窗口程序.我们在程序启动或运行过程 ...