如何扩展/覆盖插件的控制器操作?

发布于 2024-08-19 10:55:21 字数 270 浏览 8 评论 0原文

我在 grails 应用程序中使用的插件 (Nimble 0.3) 包括一些控制器和相关操作。 我想(稍微)改变一些操作行为,我想知道如何才能实现这一目标。

我可以创建一个继承自插件控制器并覆盖某些操作实现的子控制器吗?

或者,我可以创建另一个与插件控制器同名但位于不同包中的控制器吗?

实际上我真正需要理解的是:当存在名称冲突时,Grails 如何确定要调用哪个控制器操作

The plugin (Nimble 0.3) I am using in my grails application, includes some controllers and associated actions. I want to change (slightly) some actions behavior, and I was wondering how I can achieve that.

Can I create a child controller that inherits from my plugin controller and override some of the action implementation?

Or, can I create another Controller with the same name as the plugin controller but located in a different package?

Well actually what I really need to understand is : how Grails determines which controller action to call when there are name conflicts ?

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

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

发布评论

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

评论(3

归属感 2024-08-26 10:55:21

假设您有一个名为 PluginController 的插件控制器和一个要覆盖的操作“foo”,您可以对控制器进行子类化:

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

它会呈现 404 页面如果您调用旧的“未映射”控制器操作。您需要创建 grails-app/views/errors/notFound.gsp 来显示适当的 404 页面。

第一个 url 映射可确保调用您覆盖的操作。第二个将其他所有内容路由到原始控制器。第三个发送 404 进行直接访问。

Assuming you have a plugin controller named PluginController and an action 'foo' that you want to override, you can subclass the controller:

class MyController extends PluginController {

   def foo = {
      ...
   }
}

but you'll need to do some work in 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")
   }
}

and this depends on an 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'
   }
}

which renders a 404 page if you call the old "unmapped" controller actions. You'll need to create grails-app/views/errors/notFound.gsp to show an appropriate 404 page.

The first url mapping ensures that your overridden action is called. The 2nd routes everything else to the original controller. And the 3rd sends 404s for direct access.

八巷 2024-08-26 10:55:21

使用 Grails 1.3.7 和 Nimble 插件 0.4,我发现 Burt 的 UrlMapping 解决方案对我不起作用。然而,根据 Burt 的其他答案

class AuthController extends grails.plugins.nimble.core.AuthController{

    private static String TARGET = 'grails.plugins.nimble.controller.AuthController.TARGET'

    def login = {
        // custom logic here    
    }
}

With Grails 1.3.7 and Nimble plugin 0.4 I found that Burt's UrlMapping solution did not work for me. However, simply creating a controller that both subclasses the plugin controller and has the same name worked, per Burt's other answer

class AuthController extends grails.plugins.nimble.core.AuthController{

    private static String TARGET = 'grails.plugins.nimble.controller.AuthController.TARGET'

    def login = {
        // custom logic here    
    }
}
情释 2024-08-26 10:55:21

我将对原始类进行子类化并覆盖您需要的行为。我用另一个插件做了很多这样的事情,而且效果很好。

当然,另一种方法是使用内联插件并仅修改原始源,但这更糟糕,因为您会遇到升级问题。

我也是 Nimble 的用户,如果您认为您的扩展可以被其他人使用,那么您可以做出贡献 - nimble 当然试图确保它具有很强的可扩展性。

I would subclass the original class and override the behavior you need. I am doing a lot of that with another plugin, and it works great.

An alternate way is of course to use the plugin inline and just modify the original source, but that's worse, since you'll have an issue with upgrades.

I am also a user of Nimble, and if you think your extension could be used by others, then you could contribute - nimble certainly tried to make sure it's very extensible.

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