Play! Framework 学习笔记 — 注解
Interceptions拦截器注解:
@Before
所有访问的Action执行之前调用该方法,比如:
public class Admin extends Application { @Before static void checkAuthentification() { if(session.get("user") == null) login(); } public static void index() { List<User> users = User.findAll(); render(users); } ... }
public class Admin extends Application { @Before(unless="login") static void checkAuthentification() { if(session.get("user") == null) login(); } public static void index() { List<User> users = User.findAll(); render(users); } ... }
public class Admin extends Application { @Before(only={"login","logout"}) static void doSomething() { ... } ...}
public class Secure extends Controller { @Before static void checkAuthenticated() { if(!session.containsKey("user")) { unAuthorized(); } }}
@With(Secure.class)public class Admin extends Application { ... }
public class Admin extends Application { @After static void log() { Logger.info("Action executed ..."); } public static void index() { List<User> users = User.findAll(); render(users); } ... }
public class Admin extends Application { @Catch(IllegalStateException.class) public static void logIllegalState(Throwable throwable) { Logger.error("Illegal state %s…", throwable); } public static void index() { List<User> users = User.findAll(); if (users.size() == 0) { throw new IllegalStateException("Invalid database - 0 users"); } render(users); }}
public class Admin extends Application { @Finally static void log() { Logger.info("Response contains : " + response.out); } public static void index() { List<User> users = User.findAll(); render(users); } ... }