一、拦截器是用于对action请求的拦截处理,发生在进入action方法体之前的拦截操作,这样方便了对请求实例做一些文章。

 
二、自定义、系统已有拦截器都需要实现Interceptor接口,这样才能被系统认为是拦截器实现类。拦截器只有一个方法(并且只有一个传入参数ActionInvocation):
@Override
public void intercept(ActionInvocation ai) {
        System.out.println("action注入之前");
        System.out.println("actionKey:" + ai.getActionKey() + 
        "--controllerKey" + ai.getControllerKey() +
        "--methodname:" + ai.getMethodName() + 
        "--viewPath:" + ai.getViewPath());
        ai.invoke();
        System.out.println("action注入之后");
 }

 
三、如果需要使用到拦截器,首先可以实现Interceptor接口创建一个类,拦截器有三种配置级别:
1)全局拦截器:在JFinalConfig实现类的public void configInterceptor(Interceptors me)方法里添加拦截器,对所有的Controller有效;
2)Controller拦截器:通过@Before(StudentInterceptor.class)注释在类的最顶部,只对这个Controller有效;
3)Action拦截器:通过@Before(StudentValidator.class)注释在对应的action方法体上,请求该action时会先经过拦截器处理。
注意:如果需要通过注解的方式配置多个拦截器可以如下方式:@Before({StudentValidator.class, StudentValidator.class})
 
四、如果已经配置了拦截器但是又想在某个action中清除掉拦截器,可以通过注解:@ClearInterceptor(ClearLayer.ALL)清除所有的拦截器,如果没写括号参数,默认清除上一级的。
1)action清除上一级为controller级;
2)controller级别为全局级。
 
问题:如果将多个拦截器合并为一个拦截器
提示:可以查看InterceptorStack类。
 
我的代码:
1、JFinalCongig中的方法:
@Override
public void configInterceptor(Interceptors me) {
// 给所有请求加上校验,校验器也是实现了拦截器接口
me.add(new StudentValidator());
// 给所有请求加上拦截器处理
me.add(new StudentInterceptor());
 
}

2、StudentController类:
 
// controller级别粒度拦截器配置
@Before(StudentInterceptor.class)
public class StudentController extends Controller {
private static int num = 0;
 
// 设置在访问此方法时先被拦截器拦截处理,此处配置的拦截器粒度为Action
@Before(StudentInterceptor.class)
public void index() {
System.out.println("------------start index------------");
List list = Student.dao.find("select * from student");
setAttr("studentList", list);
render("/index.html");
System.out.println("------------end index------------");
}
 
// 可以不根据方法名作为访问该方法的url,用actionkey可以自定义url
@ActionKey("/test")
public void add() {
System.out.println("------------start add------------");
List classesList = Classes.dao.find("select * from classes");
setAttr("classesList", classesList);
render("/add.html");
System.out.println("------------end add------------");
}
 
public void delete() {
System.out.println("------------start delete------------");
// 获取表单域名为studentID的值
// Student.dao.deleteById(getPara("studentID"));
// 获取url请求中第一个值
Student.dao.deleteById(getParaToInt());
forwardAction("/student");
System.out.println("------------end delete------------");
}
 
public void update() {
System.out.println("------------start update------------");
Student student = getModel(Student.class);
student.update();
forwardAction("/student"); // 后台直接action跳转,无需前台再发一次请求
//redirect("/student"); // 重定向url前台再发一次请求
System.out.println("------------end update------------");
}
 
// 清除所有的拦截器配置,默认为删除其上一级别的拦截器
@ClearInterceptor(ClearLayer.ALL)
public void get() {
System.out.println("------------start get------------");
Student student = Student.dao.findById(getParaToInt());
setAttr("student", student);
render("/change.html");
System.out.println("------------end get------------");
}
 
// 设置在进行保存时先对保存的内容进行校验,校验不成功直接返回(在validator中实现)
@Before({StudentValidator.class, StudentValidator.class})
public void save() {
System.out.println("------------start save------------");
Student student = getModel(Student.class);
student.set("studentid", num++).save();
forwardAction("/student");
System.out.println("------------end save------------");
}
}

jfinal中Interceptor拦截器的使用相关推荐

  1. 框架:SpringMVC中Interceptor拦截器的两种实现

    Spring中使用Interceptor拦截器 SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证, ...

  2. spring拦截器覆盖_Spring中使用Interceptor拦截器

    SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...

  3. SpringMVC中使用Interceptor拦截器

    2019独角兽企业重金招聘Python工程师标准>>> SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理. ...

  4. Springmvc中的拦截器interceptor及与过滤器filter的区别

    一.Springmvc中的拦截器概述及与过滤器filter的区别 1).Springmvc中的拦截器interceptor用于对控制器controller进行预处理和后处理的技术; 2).可以定义拦截 ...

  5. 在SpringMVC中使用拦截器(interceptor)拦截CSRF***

    关于什么是CSRF我这里就不多说了,以前转载的一篇文章(PS:https://www.zifangsky.cn/358.html)已经说得很清楚了.这里只是简单介绍如何在SpringMVC中使用拦截器 ...

  6. 四十七、Kafka中的拦截器(Interceptor)

    前两篇文章我们分别介绍了Kafka生产者和消费者的API,本文我们介绍一下Kafka中拦截器的知识.关注专栏<破茧成蝶--大数据篇>,查看更多相关的内容~ 目录 一.拦截器介绍 二.拦截器 ...

  7. 谈谈spring中的拦截器interceptor

    谈谈spring中的拦截器 在web开发中,拦截器是经常用到的功能.它可以帮我们验证是否登陆.预先设置数据以及统计方法的执行效率等等.今天就来详细的谈一下spring中的拦截器.spring中拦截器主 ...

  8. SpringMVC中的拦截器

    SpringMVC中的拦截器 拦截器的作用 Spring MVC 的处理器拦截器类似于 Servlet 开发中的过滤器 Filter,用于对处理器进行预处理和后处理. 用户可以自己定义一些拦截器来实现 ...

  9. springMVC之Interceptor拦截器

    转自:https://blog.csdn.net/qq_25673113/article/details/79153547 Interceptor拦截器用于拦截Controller层接口,表现形式有点 ...

  10. interceptor拦截器典型应用实例----数据稽核

    interceptor拦截器典型应用实例----数据稽核 Interceptor拦截器的使用小结,程序实例是典型的 "数据稽核"过程,即在对数据库中的数据进行修改后会自动添加对应的 ...

最新文章

  1. c语言程序设计自评报告,石家庄学院c语言程序设计自评报告.docx
  2. SSL 1055——能量项链_DP
  3. 052_Function对象
  4. 大一计算机课程ppt作业,大学生计算机基础作业PPT.ppt
  5. POJ 1703 Find them, Catch them
  6. A标签使用javascript:伪协议
  7. python_循环删除list中的元素,有坑啊!
  8. centos7公司内网环境搭建集群性能测试环境(ip+域名部署)
  9. linux删除磁盘后刷新,linux – 短暂的文件是否刷新到磁盘?
  10. StringBuffer append整数0001的问题
  11. 好程序员教程分析Vue学习笔记五
  12. pyspider all 只启动了_我是如何让微博绿洲的启动速度提升30%的(二)
  13. 【Java 强化】单元测试(JUnit3、JUnit4)、XML(语法、约束、文档结构)、DOM、DOM4J
  14. AndroidL 开机展示Keyguard锁屏机制初探
  15. 白素雅 中国科学院大学 计算机应用技术,张玉清 - 中国科学院大学 - 计算机科学与技术学院...
  16. 金蝶K3退出远程桌面后,客户端无法登陆
  17. JXTA第一步:HelloWorld
  18. 手机自带抖音无水印视频下载功能,另推荐抖音短视频去水印网页版
  19. ZigBee模块无线通信组网结构技术之Mesh拓扑网状
  20. NV 3D Vision

热门文章

  1. Atitit 深入了解UUID含义是通用唯一识别码 (Universally Unique Identifier),
  2. Atitit.java图片图像处理attilax总结
  3. Atitit.excel导出 功能解决方案 php java C#.net版总集合.doc
  4. Atitit.sql where条件表达式的原理  attilax概括
  5. atitit.软件开发概念--过滤和投影 数据操作
  6. paip java.net.SocketException No buffer space available的解决办法及总结
  7. paip.c++ qt creator svn 设置以及使用总结.
  8. paip.c#.net自定义图像窗体form
  9. paip.网站扫描安全工具hp WebInspect 使用指南
  10. paip.提升用户体验---搜索功能设计