如何扩展/覆盖插件的控制器操作?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您有一个名为 PluginController 的插件控制器和一个要覆盖的操作“foo”,您可以对控制器进行子类化:
但是您需要在 UrlMappings: 中做一些工作:
这取决于 ErrorsController:
它会呈现 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:
but you'll need to do some work in UrlMappings:
and this depends on an ErrorsController:
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.
使用 Grails 1.3.7 和 Nimble 插件 0.4,我发现 Burt 的 UrlMapping 解决方案对我不起作用。然而,根据 Burt 的其他答案
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
我将对原始类进行子类化并覆盖您需要的行为。我用另一个插件做了很多这样的事情,而且效果很好。
当然,另一种方法是使用内联插件并仅修改原始源,但这更糟糕,因为您会遇到升级问题。
我也是 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.