Grails:views 文件夹中的 Javascript 文件
我想将 Grails 中的视图拆分为 2 个文件,一个 .gsp 文件和一个 .js 文件,以便将 Javascript 与我的视图更清晰地分离。 所以这是一个例子:
views/index.gsp
views/index.js
views/home/index.jsp
views/home/index.js
但是当我像这样简单地添加index.js脚本引用时:
<script src="index.js" type="text/javascript"></script>
我得到的只是404。
有谁知道如何处理这个问题?
一个很大的好处是能够使用 index.js 文件内的视图数据来生成所需的内容。
马蒂亚斯.
I'd like to split my views in Grails into 2 files a .gsp file and a .js file so that I get a cleaner Javascript separation from my views. So here's an example:
views/index.gsp
views/index.js
views/home/index.jsp
views/home/index.js
But when I simply add the index.js script reference like this:
<script src="index.js" type="text/javascript"></script>
all I get is a 404.
Does anyone knows how to deal with this?
A great benefit would be to have the ability to use view data inside the index.js file to produce the desired content.
Matthias.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实际上,完全可以从您的 grails-app/views/ 目录中将 JS 文件(或任何其他文件类型)作为 GSP 提供服务。 你唯一要做的就是为这些 GSP 定义一个合适的 URL 映射,例如:
通过这个 URL 映射,你可以将 JS 代码放入
grails-app/views/home/index.js.gsp (注意后面的 .gsp),您可以在 JS 源中使用任何 grails 标签。 为了确保您的 JS 以正确的内容类型交付,您可能需要将其放置
在 GSP 的开头。
不幸的是,
createLink
标记不支持将链接重写为视图,但编写您自己的标记来创建这些链接应该很容易。无论如何,请记住,这不会对您的应用程序的性能产生非常积极的影响。 例如,通常最好拥有静态 JS 文件(并将它们作为静态资源),同时将动态内容作为参数传递给 JS 函数。 这也将使您免受一些头痛的困扰。 缓存等
Actually, it should be perfectly possible to serve a JS file (or any other file type) as a GSP from your
grails-app/views/
directory. The only thing you have to do, is define a suitable URL mapping for those GSPs, e.g.:With this URL mapping, you can put your JS code into
grails-app/views/home/index.js.gsp
(note the trailing .gsp) and you can use any grails tags in your JS source. To ensure that your JS is delivered with the correct content type, you may want to placeat the beginning of your GSP.
Unfortunately, the
createLink
tag doesn't support link rewriting to views, but it should be easy to write your own tag to create those links.Anyways, keep in mind that this won't have a very positive impact on your app's performance. It's usually better to have static JS files (and also serve them as static resources) while passing dynamic stuff as parameters to JS functions for example. This will also keep you from some headaches wrt. caching etc.
这个想法很好,但是 Grails 拥有这种目录结构是有原因的。 视图文件夹适用于某种工件类型(视图)。
您可以在 web-inf 下克隆视图文件夹结构,但这会给您带来更多工作,因为我猜这背后的想法是为了方便起见,将相关文件放在一起。 。
尽管我对将 Javascript 与视图存储在一起并不感到兴奋,但我喜欢 Robert 的想法,即通过使用构建事件将 javascript 源复制到正确的目录来挂钩构建过程! 如果您决定走这条路,您不妨在这样做时压缩源代码。 ShrinkSafe 是流行的库。
The idea is good, but Grails has this directory structure for a reason. The view folder is intended for a certain artifact type (views)..
You could clone your view folder structure under web-inf, but that gives you more work as I guess the idea behind this is to keep related files close together for convenience reasons.
Even though I'm not to excited about storing Javascript together with the view I loved Robert's idea of hooking into the build process by using build events to copy javascript sources into the right directory! If you decide to go down that road you might as well compress the sources while you're at it. ShrinkSafe is popular library.
我不认为你被允许访问视图/内部的js,
如果你需要这样做......这是
输入 js
... write down you js in here ...
输入这个标签来上传你的js
I don't think you are allowed to access js inside views/
if you need to do that ... here is the trick
iniside _myjs.gsp type you js
... write down you js in here ...
type this tag to upload you js
更新 2:
Grails 提供了使用 自定义事件。
可以编写一个事件处理程序,将
grails-app/views
下的所有 JavaScript 文件与web-app/js
的目标文件夹同步。将自定义代码放入
$PROJECT/scripts/Events.groovy
中。PackagingEnd
是一个很好的调用目标,因为它发生在web.xml
生成之后。更新
如果您希望 JavaScript 文件简单地“啮合”在一起,您可以使用符号链接来做到这一点,例如:
据我所知,没有办法强制 grails 直接提供内容在网络应用程序之外。
或者,您可以内联 JavaScript,但这可能会影响性能。
JavaScript 文件位于
web-app/js
下。然后您可以使用
引用它们。Update 2:
Grails offer the possibility of hooking into the build lifecycle using custom events.
An event handler can be written which synchronises all JavaScript files under
grails-app/views
with the target folder ofweb-app/js
.Place the custom code in
$PROJECT/scripts/Events.groovy
. ThePackagingEnd
is a good target for the invocation, since it happens right afterweb.xml
is generated.Update
If you'd like the JavaScript files simply 'meshed' together, you can do that using symlinks, e.g.:
As far as I know, there is no way of forcing grails to directly serve content which is outside of web-app.
Alternatively, you can inline your JavaScript, but that can have performance implications.
JavaScript files belong under
web-app/js
.Then you can reference them using
<g:javascript src="index.js" />
.