Grails:如何引用位于已安装插件内的资源? [英] Grails: How to reference a resource located inside an installed plugin?

查看:91
本文介绍了Grails:如何引用位于已安装插件内的资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Grails插件,我用它作为一些共享静态资源的容器,这些资源将被其他各种Grails项目(CSS,JS文件等)使用。使用插件的想法只是为了分享这些东西 - 这个插件并没有真正做其他任何有趣的事情。直接引用插件内部资源的最佳方式是什么?我宁愿不创建自定义taglibs,因为我没有真正添加任何功能。我只想访问插件中的资源 - 像这样,例如,引用来自GSP的CSS文件:

  < g:link rel =stylesheeturl =$ {resource(dir:'css',file:'mycss.css',
plugin:'myplugin')}/>

这个想法是否违反了插件的概念,只有某些方面应该被暴露,而休息是一个黑匣子?在相关的Grails项目中分享静态资源的其他方法也是受欢迎的,以防我通过尝试这种方式走向疯狂的错误路径。 :)

解决方案

首先,在Grails中,插件不被视为黑盒子。相反,所有的代码都是暴露的;如果你真的想把它变成一个黑盒子,那么你应该使用另一个插件,二进制文件

其次,您的方法非常明智。要访问插件资源,我会创建一个taglib,例如:

  def res = {attrs  - > 
attrs.dir = pluginContextPath
//在这里做任何你想做的事
out<< g.resource(attrs)
}

并将其与< G:资源> 。这样,你甚至不必公开你的插件的资源路径。 Nimble插件正在使用这种方法。



你也可以看看 Grails Resources插件,它试图处理Grails插件和项目之间的静态资源依赖关系的噩梦。我从来没有使用它,但肯定在我的下一个Grails项目中,我将整合它(顺便说一下,它将被包含到Grails 1.4中)。

I have a Grails plugin which I’m using as basically a container for some shared static resources that will be used by various other Grails projects in-house (CSS, JS files, etc). The idea of using a plugin was just to share these things — the plugin doesn’t really do anything else very interesting. What is the best way to directly reference resources inside the plugin? I’d rather not create custom taglibs, since I’m not really adding any functionality here; I just want access to the resources inside the plugin — something like this, e.g., to reference a CSS file from a GSP:

<g:link rel="stylesheet" url="${resource(dir:'css', file:'mycss.css',
plugin:'myplugin')}"/>

Does this idea violate the concept of plugins, where only certain aspects of it should be exposed, and the rest is a black box? Alternative approaches to sharing static resources among related Grails projects are also welcome, in case I’m headed down an insane, wrong-headed path by even trying this. :)

解决方案

First, in Grails, a plugin is not considered a black box. To the contrary, all the code is exposed; if you really want to make it a black box, then you should use another plugin, Binary Artifacts.

Secondly, your approach is very sensible. To access plugin resources, I would have created a taglib like:

def res = { attrs -> 
  attrs.dir=pluginContextPath
  //Do whatever you want here
  out << g.resource(attrs) 
}

and call it the same way as <g:resource>. That way, you don’t even have to expose your plugin’s resources path. The Nimble plugin is using this approach.

Also you might have a look at the Grails Resources plugin, which is trying to handle the nightmare of static resource dependencies among Grails plugins and projects. I have never used it, but for sure in my next Grails project, I will integrate it (BTW, it will be included into Grails 1.4).

这篇关于Grails:如何引用位于已安装插件内的资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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