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

新建一个MyIntercept

package com.action;import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;public class MyInterceptor implements Interceptor{public void destroy() {// TODO Auto-generated method stubSystem.out.println("销毁拦截器");}public void init() {// TODO Auto-generated method stubSystem.out.println("初始化拦截器");}public String intercept(ActionInvocation invocation) throws Exception {// TODO Auto-generated method stubSystem.out.println("进入拦截器");if(条件判断)return invocation.invoke();//表示放行else{return "error";}}
}

配置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="demo" namespace="/demo" extends="struts-default"><span style="color:#FF0000;"><interceptors><interceptor name="MyInterceptor" class="com.action.MyInterceptor" /><interceptor-stack name="InterceptorStack"><interceptor-ref name="defaultStack" /><interceptor-ref name="MyInterceptor" /></interceptor-stack></interceptors></span><action name="demo" class="com.action.HelloWorld" method="execute"><result name="succcess">/index.jsp</result><result name="error">/error.jsp</result><span style="color:#FF0000;"><interceptor-ref name="MyInterceptor"/></span></action> </package></struts>

因为Struts2中如文件上传,数据验证,封装请求参数到action等功能都是由系统默认的defaultStack中的拦截器实现的,所以我们定义的拦截器需要引用系统默认的defaultStack,这样应用才可以使用Struts2框架提供的众多功能。
           如果希望包下的所有action都使用自定义的拦截器,可以通过<default-interceptor-ref name=“permissionStack”/>把拦截器定义为默认拦截器。
           注意:每个包只能指定一个默认拦截器。
           另外,一旦我们为该包中的某个action显式指定了某个拦截器,则默认拦截器不会起作用。

转载于:https://www.cnblogs.com/laohuihui/p/5308761.html

Struts2 自定义拦截器(easy example)相关推荐

  1. struts2自定义拦截器并配置拦截器使其生效

    首先编写一个struts2的拦截器,要继承 MethodFilterInterceptor 并获取其中的方法 package star.july.d_interceptor;import com.op ...

  2. Struts2 自定义拦截器(方法拦截器)

    转自:http://05061107cm.iteye.com/blog/365504 struts2系统自带了很多拦截器,有时需要我们自己定义,一般有两种方式: 一.实现Interceptor接口 J ...

  3. struts2自定义拦截器一——模拟登陆权限验证

    1.http://localhost:8083/struts2/user.jsp 表示用户已登陆,存放session对象 2.http://localhost:8083/struts2/quit.js ...

  4. Struts2自定义拦截器

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

  5. Struts2 自定义拦截器

    所属栏目:AbstractInterceptor 如果要编写自定义拦截器,那么可以只用实现com.opensymphony.xwork2.interceptor.Interceptor这个接口即可. ...

  6. struts2自定义拦截器(登陆校验拦截器)

    需求:有些页面需要先登录才可以访问,如果没有登陆就跳转到login.jsp去登陆 此案例为访问login方法的时候不拦截,只有把user放到session中才可以访问别的方法 web.xml 配置拦截 ...

  7. Struts2自定义拦截器实例—登陆权限验证

    版本:struts2.1.6 此实例实现功能:用户需要指定用户名登陆,登陆成功进入相应页面执行操作,否则返回到登陆页面进行登陆,当直接访问操作页面(登陆后才能访问的页面)时则不允许,须返回登陆页面. ...

  8. dwz ajax session超时跳转登录页(struts2自定义拦截器)

    1.定义struts2拦截器(网上例子很多) 代码如下: package rt.intercepter;import java.util.Map;import javax.servlet.http.H ...

  9. Struts2自定义类型转换器、自定义拦截器和用户输入数据的验证

    一.自定义类型转换器 1.编写一个类,继承com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter 2.覆盖掉其中的public Obj ...

最新文章

  1. Format specifies type 'id' but the argument has type 'NSError *__autoreleasing *
  2. AI一分钟|腾讯与三星结盟,以对抗苹果和谷歌;亚马逊回应“AI误将国会议员标记为罪犯”...
  3. VS中一些提高编码效率的快捷键
  4. 以太网性能测试分析仪
  5. 《SAS编程与数据挖掘商业案例》学习笔记之十三
  6. java与java ee_使用Java EE的ManagedExecutorService异步执行事务
  7. 理论基础 —— 排序 —— 基数排序
  8. 罗永浩:比起悲剧英雄 我更喜欢被当成失败的小丑
  9. CentOS-6.4 安装 PHP Memcached 扩展
  10. vmVare使用NAT模式-配置详情
  11. 发现这里才是我的地方
  12. lvs、lvdisplay、lvscan 查看逻辑卷
  13. 《R语言初学者指南》pdf
  14. 银河麒麟Linux系统安装谷歌浏览器
  15. U盘拷贝者MBR勒索木马分析
  16. 百宝云COM组件操作教程
  17. ros参数服务器调用_ROS路由与秒开缓存服务器对接时如何设置使用
  18. python转pyd
  19. dataturks解析
  20. 无法登陆github官网的问题(能ping通但打不开网页)

热门文章

  1. CTF常用python库PwnTools的使用学习
  2. 根本不存在 DIV + CSS 布局这回事
  3. 天空之城 matlab,[转载]matlab演奏《天空之城》代码
  4. python3虚拟环境使用教程_python虚拟环境完美部署教程
  5. jedis使用_Redis的三个框架:Jedis,Redisson,Lettuce
  6. 机器人教育发展_得于人工智能发展,机器人教育低龄化越来越普及
  7. 爬虫软件python彻底卸载_Python爬虫实践:如何优雅地删除豆瓣小组的帖子
  8. elementUI vxe-table结合使用(通用表格)
  9. linux xargs命令,xargs 命令教程
  10. 指纹识别工具(CMSeek)