天天看点

jfinal中Interceptor拦截器的使用

一、拦截器是用于对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中的方法:

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

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.set("studentid", num++).save();

System.out.println("------------end save------------");

继续阅读