我在 grails 应用程序中使用的插件 (Nimble 0.3) 包括一些 Controller 和相关操作。 我想(稍微)改变一些 Action 行为 ,我想知道如何才能做到这一点。
我可以创建一个 子 Controller 从我的插件 Controller 继承并覆盖一些操作实现?
或者,我可以创建另一个 Controller 同名 作为插件 Controller 但位于不同的包中?
实际上我真正需要了解的是: Grails 如何确定调用哪个 Controller 操作 什么时候有名字冲突?
最佳答案
假设您有一个名为 PluginController 的插件 Controller 和一个要覆盖的操作“foo”,您可以将 Controller 子类化:
class MyController extends PluginController {
def foo = {
...
}
}
但是你需要在 UrlMappings 中做一些工作:
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?" {
constraints {}
}
"/myController/foo/$id?"(controller: "myController", action: "foo")
"/myController/$action?/$id?"(controller: "pluginController")
"/pluginController/$action?/$id?"(controller: "errors", action: "urlMapping")
"/"(view:"/index")
"500"(view:'/error')
"404"(controller: "errors", action: "notFound")
}
}
这取决于 ErrorsController:
class ErrorsController {
def notFound = {
log.debug "could not find $request.forwardURI"
}
def urlMapping = {
log.warn "unexpected call to URL-Mapped $request.forwardURI"
render view: 'notFound'
}
}
如果您调用旧的“未映射” Controller 操作,它将呈现 404 页面。您需要创建 grails-app/views/errors/notFound.gsp 以显示适当的 404 页面。
第一个 url 映射可确保调用您的覆盖操作。第二个将其他所有内容路由到原始 Controller 。第三个发送 404s 进行直接访问。
关于model-view-controller - 如何扩展/覆盖插件的 Controller 操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2173795/