model-view-controller - 如何扩展/覆盖插件的 Controller 操作?

标签 model-view-controller inheritance grails grails-plugin grails-controller

我在 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/

相关文章:

json - Ext Ajax请求未从Grails Controller 接收JSON作为response.responseText

mongodb - Search 在 MVC 软件模式中处于什么位置?

c# - Func<T> 的性能和继承

C++ 错误 C2248 : cannot access private member declared in SUPER class

javascript - Node.js 中的继承函数如何工作?

ubuntu - 在 ubuntu 14.04 服务器下,双数乘法在 Grails 中不起作用

c# - 如何通过 mvc sql server 中另一个下拉列表的值填充下拉列表

javascript - 想要附加 URL 以及想要使用 HTML 的 Get 请求提交表单

java - service层和controller在实践中的区别

grails - 通过 grails spring security facebook 插件登录后重定向?