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

发布于 2024-09-29 11:12:30 字数 424 浏览 4 评论 0原文

伪代码:

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)
  }
}

有什么建议吗?

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

Pseudo-code:

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)
  }
}

Any suggestions?

Edit: Because the question is a bit dry you could try to spice up your answers with some experience with the framework and what do you think is better with that approach.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

戈亓 2024-10-06 11:12:30

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
}

虽然将控制器注入其他控制器不是常见做法,但您可以注入任何对象)

Spring MVC and Grails (built ontop of spring) favour dependency injection with no inheritance whatsoever. Each controller is a class that does not extend anything. Instead you can inject other components into it (using dependency-injection). For example:

@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
}

(Although it is not a common practice to inject controllers into other controllers, you can inject any object)

荆棘i 2024-10-06 11:12:30
奢欲 2024-10-06 11:12:30

Django 决定对 MVC 模式使用不同的术语。在 django 中,“视图”是大多数人所说的控制器。 django 视图只是一个获取请求实例并返回响应实例的函数。大致看起来是这样的:

from django.http import HttpResponse, Http404
import datetime

def current_datetime(request):
    if request.method != 'GET':
        raise Http404
    now = datetime.datetime.now()
    html = "<html><body>It is now %s.</body></html>" % now
    return HttpResponse(html)

Django has decided to use different terms for the MVC pattern. In django-speak "view" is what most people call controller. a django view is just a function taking a request instance and returning a response instance. Roughly it looks like this:

from django.http import HttpResponse, Http404
import datetime

def current_datetime(request):
    if request.method != 'GET':
        raise Http404
    now = datetime.datetime.now()
    html = "<html><body>It is now %s.</body></html>" % now
    return HttpResponse(html)
阪姬 2024-10-06 11:12:30

JSF& Spring MVC

JSF 的简单教程:www.coreservlets.com/JSF-Tutorial/jsf2/index.html#Annotations

SpringSource 的基本示例:http://src.springframework.org /svn/spring-samples/mvc-basic/trunk

问:为什么选择 JSF

答:
您可以在 SimpleController#doSomething 处执行操作

@ManagedBean 
public class SimpleController {  

  public String doSomething() {
    ...
  }

}

,并且 SimpleController 不会扩展任何控制器,@ManagedBean 有助于使其看起来像控制器。

问:为什么选择 Spring MVC


您可以在“.../doSomething”处执行操作

@Controller
public class SimpleController {  

      @RequestMapping("/doSomething")
  public String doSomething() {
    ...
  }

}

SimpleController 不扩展任何控制器。

  • @Controller 有助于使该类成为控制器
  • 绑定到“/doSomething”

@RequestMapping 将其url

JSF & Spring MVC

An easy tutorial for JSF: www.coreservlets.com/JSF-Tutorial/jsf2/index.html#Annotations

A basic example from SpringSource: http://src.springframework.org/svn/spring-samples/mvc-basic/trunk

Q: Why JSF

A:
You can perform your action at SimpleController#doSomething

@ManagedBean 
public class SimpleController {  

  public String doSomething() {
    ...
  }

}

And SimpleController does not extend any controller, @ManagedBean helps to make it look like a controller.

Q: Why Spring MVC

A:
You can perform your action at "..../doSomething"

@Controller
public class SimpleController {  

      @RequestMapping("/doSomething")
  public String doSomething() {
    ...
  }

}

SimpleController does not extend any controller.

  • @Controller helps to make the class a controller
  • @RequestMapping bind its url to "/doSomething"

[sorry for bold url, i can only post maximum one url in an answer as a newbie :-S]

み格子的夏天 2024-10-06 11:12:30

Struts 2 也可以让您做到这一点。所有的动作都是简单的java bean。

Struts 2 also lets you do this. All the actions are simple java beans.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文