如何扩展/重写插件的控制器操作? [英] How to extend/override controller actions of plugins?

查看:177
本文介绍了如何扩展/重写插件的控制器操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在grails应用程序中使用的插件(Nimble 0.3)包含一些控制器和相关的操作。 我想改变(稍微)一些动作行为,我在想如何才能做到这一点。



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



或者,我可以创建另一个具有相同名称的控制器 / strong>作为插件控制器,但位于不同的包中?



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

解决方案

假设你有一个名为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)


$ / code $ / pre

这取决于一个ErrorsController:

  class ErrorsController {

def notFound = {
log.debug找不到$ request.forwardURI


def urlMapping = {
log.warn意外调用URL映射的$ request.forwardURI
渲染视图:'notFound'
}
}

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



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


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 ?

解决方案

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.

这篇关于如何扩展/重写插件的控制器操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆