建议不使用控制器基类的MVC框架

c0r*_*li0 2 language-agnostic model-view-controller design-patterns web-applications

伪代码:

class SomeController {
  def myAction() {
    // controler is an property passed via ctor
    controller.redirect(toWhereever)
  }
}

// another variant
class AnotherController {
  def myAction(controller) {
    // controler is an method argument
    controller.redirect(toWhereever)
  }
}
Run Code Online (Sandbox Code Playgroud)

有什么建议?

编辑:因为问题有点干,你可以尝试用框架的一些经验来增加你的答案,你认为这种方法更好.

Boz*_*zho 7

Spring MVCGrails(构建在spring上)支持依赖注入,无任何继承.每个控制器都是一个不扩展任何东西的类.相反,您可以将其他组件注入其中(使用依赖注入).例如:

@Controller
@RequestMapping("/user")
public class UserController {

     @Inject
     private UserService service;

     @RequestMapping("/register")
     public String register(User user) {..}

}

@Controller
@RequestMapping("/orders")
public class OrderController {
     @Inject
     private UserController userController
}
Run Code Online (Sandbox Code Playgroud)

(虽然将控制器注入其他控制器并不常见,但可以注入任何对象)