建议不使用控制器基类的 MVC 框架
伪代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Spring MVC 和 Grails(构建在 spring 之上)支持依赖注入,没有任何继承。每个控制器都是一个不扩展任何内容的类。相反,您可以将其他组件注入其中(使用依赖注入)。例如:(
虽然将控制器注入其他控制器不是常见做法,但您可以注入任何对象)
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:
(Although it is not a common practice to inject controllers into other controllers, you can inject any object)
Django 决定对 MVC 模式使用不同的术语。在 django 中,“视图”是大多数人所说的控制器。 django 视图只是一个获取请求实例并返回响应实例的函数。大致看起来是这样的:
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:
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 处执行操作
,并且 SimpleController 不会扩展任何控制器,@ManagedBean 有助于使其看起来像控制器。
问:为什么选择 Spring MVC
?
您可以在“.../doSomething”处执行操作
SimpleController 不扩展任何控制器。
@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
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"
SimpleController does not extend any controller.
[sorry for bold url, i can only post maximum one url in an answer as a newbie :-S]
Struts 2 也可以让您做到这一点。所有的动作都是简单的java bean。
Struts 2 also lets you do this. All the actions are simple java beans.