Intercept
The WebMVC module is based on the new features of YMPv2.0. It natively supports AOP method interception and is configured through the following annotations:
@Before: used to set the prefix of a class or method Interceptor, the pre-interceptor declared on the class will be applied to all methods of the class;
@After: used to set the post-interceptor of a class or party, the post-interceptor declared on the class Set interceptors will be applied to all methods of the class;
@Clean: used to clean all or specified interceptors on the class, the cleaned interceptors will not be executed;
@ContextParam: used to set context parameters, mainly used to pass parameter configurations to interceptors;
@Ignored: declaring a method will ignore all interceptor configurations;
Description: Declaring @Ignored annotated methods, non-public methods, Object class methods and Object class overloaded methods will not be processed by the interceptor.
Sample code:
// 创建自定义拦截器 public class UserSessionChecker implements IInterceptor { public Object intercept(InterceptContext context) throws Exception { // 判断当前拦截器执行方向 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"); } }