YMP Online Manual
/ 攔截器(Intercept)
攔截器(Intercept)
WebMVC模塊基于YMPv2.0的新特性,原生支持AOP方法攔截,通過(guò)以下注解進(jìn)行配置:
@Before:用于設(shè)置一個(gè)類或方法的前置攔截器,聲明在類上的前置攔截器將被應(yīng)用到該類所有方法上;
@After:用于設(shè)置一個(gè)類或方的后置攔截器,聲明在類上的后置攔截器將被應(yīng)用到該類所有方法上;
@Clean:用于清理類上全部或指定的攔截器,被清理的攔截器將不會(huì)被執(zhí)行;
@ContextParam:用于設(shè)置上下文參數(shù),主要用于向攔截器傳遞參數(shù)配置;
@Ignored:聲明一個(gè)方法將忽略一切攔截器配置;
說(shuō)明: 聲明@Ignored注解的方法、非公有方法和Object類方法及Object類重載方法將不被攔截器處理。
示例代碼:
// 創(chuàng)建自定義攔截器 public class UserSessionChecker implements IInterceptor { public Object intercept(InterceptContext context) throws Exception { // 判斷當(dāng)前攔截器執(zhí)行方向 if (context.getDirection().equals(Direction.BEFORE) && WebContext.getRequest().getSession(false) == null) { return View.redirectView("/user/login"); } return null; } } @Controller @RequestMapping("/user") @Before(UserSessionChecker.class) public class Controller { @RequestMapping("/center") public IView userCenter() throws Exception { // ...... return View.jspView("/user/center"); } @RequestMapping("/login") @Clean public IView userLogin() throws Exception { return View.jspView("/user/login"); } }