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中的方法:
@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------------");
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM