3.2 Action

1、Action 定义

在 Controller 之中定义的 public 方法称为Action。Action 是请求的最小单位。Action 方法必须在 Controller 中定义,且必须是 public 可见性。

  1. public class HelloController extends Controller {
  2. public void index() {
  3. renderText("此方法是一个action");
  4. }
  5. public String test() {
  6. return "index.html";
  7. }
  8. }

以上代码中定义了两个Action:HelloController.index()、HelloController.test()。

Action可以有返回值,返回值可在拦截器中通过invocation.getReturnValue() 获取到,以便进行render控制。

2、@NotAction 注解

如果希望 controller 中的 public 方法不成为一个 action,可以使用 @NotAction 注解。@NotAction 注解通常用于引入了 BaseController 的中间 Controller,例如:

  1. public class BaseController extends Controller {
  2.  
  3. // 不希望成为 action,仅供子类调用,或拦截器中调用
  4. @NotAction
  5. public void getLoginUser() {
  6.  
  7. }
  8. }

3、控制器超类的路由映射

自 jfinal 3.6 开始,控制器超类中的所有方法默认不会被映射为 action。(也就是自 jfinal 3.6 版本开始上例中 BaseController 中的 @NotAction 默认已经不需要了,因为 BaseController 是你最终控制器 XxxController 的超类)

如果希望超类中的方法也被映射为 action 只需添加一行配置:

  1. public void configRoute(Routes me) {
  2. me.setMappingSuperClass(true);
  3. }

该功能属于性能优化,拥有大量路由的大型项目可加快启动速度。该配置如果配置在 "子Routes" 中,将只对该 "子Routes" 有效,例如:

  1. public FrontRoutes extends Routes {
  2. public void config() {
  3. // 这里配置只对 FrontRoutes 下的路由有效,建议这样配置以提升性能
  4. setMappingSuperClass(true);
  5.  
  6. add("/weixin", WeixinController.class);
  7. }
  8. }

"子Routes" 相关内容详见文档第二章 configRoute(…) 章节。