[英]Where should I put javascript libraries in a Grails app?
I have a couple of Javascript libraries of my own that do some nice prettyfying of my HTML pages and I want to include them in my gsp pages, particularly the landing page of my app. 我有几个自己的Javascript库,可以很好地完善我的HTML页面,我希望将它们包含在我的gsp页面中,特别是我的应用程序的登陆页面。 I have tried putting them in
views
folder and web-app/js
and $APP_HOME/scripts
but when I load my index.gsp the scripts don't show up. 我已经尝试将它们放在
views
文件夹和web-app/js
以及$APP_HOME/scripts
但是当我加载index.gsp时脚本没有显示出来。
I have also tried a variety of alternatives in my code none of which work... 我也在我的代码中尝试了各种替代方案,但都没有...
<script src="mylib.js" type="text/javascript"></script>
<script src="js/mylib.js" type="text/javascript"></script>
<script src="scripts/mylib.js" type="text/javascript"></script>
I'm sure there is a clever grails way of doing this on the fly, but I really just want a location where I can place some boilerplate JavaScript code that I can use in my app. 我确信有一种聪明的grails方式可以实现这一点,但我真的只想要一个位置,我可以放置一些样板代码,我可以在我的应用程序中使用。 With convention over configuration in mind, what is the expected practice?
考虑到约定而不是配置,预期的做法是什么?
You should probably NOT use <g:javascript library="myLib" />
as that is meant to be used as a way to make AJAX calls library (scriptaculous, dojo, yahoo, jquery) indifferent. 您可能不应该使用
<g:javascript library="myLib" />
因为它可以用来使AJAX调用库(scriptaculous,dojo,yahoo,jquery)无动于衷。 See grails javascript . 查看grails javascript 。 Instead use
<g:javascript src="myLib.js" />
. 而是使用
<g:javascript src="myLib.js" />
。
使用您的JS文件: web-app/js/myLib.js
,在您的布局中放置<g:javascript library="myLib" />
应该是我认为您需要的。
There are two ways by which you can include JS file and JS library like scriptaculous 有两种方法可以包含JS文件和JS库,如scriptaculous
// actually imports '/app/js/myscript.js'
<g:javascript src="myscript.js" />
// imports all the necessary js for the scriptaculous library
<g:javascript library="scriptaculous" />
<g:javascript>alert('hello')</g:javascript>
Refer : http://grails.org/doc/latest/ref/Tags/javascript.html 请参阅: http : //grails.org/doc/latest/ref/Tags/javascript.html
Use <g:javascript src="myscript.js" />
and place your script in web-app/js/myscript.js
使用
<g:javascript src="myscript.js" />
并将脚本放在web-app/js/myscript.js
For full detail and examples for both your own external scripts, in-line code and libraries see http://grails.org/doc/latest/ref/Tags/javascript.html . 有关您自己的外部脚本,内联代码和库的完整详细信息和示例,请参阅http://grails.org/doc/latest/ref/Tags/javascript.html 。
I think I found the answer... 我想我找到了答案......
If I use this tag in my gsp (rather than a straightforward javascript reference) 如果我在我的gsp中使用此标记(而不是简单的javascript参考)
<g:javascript library="mylib" />
Then when I look at the generated page source it refers to 然后,当我查看生成的页面源时,它指的是
<script type="text/javascript" src="/myapp/js/mylib.js"></script>
This corresponds to the folder web-app/js
, so I dropped my script library in there and it works fine. 这对应于文件夹
web-app/js
,所以我把我的脚本库放在那里,它工作正常。 The breakthrough was for me to a) rename my index.html to index.gsp and b) use the g:javascript
tag. 我突破了以下方面:a)将index.html重命名为index.gsp,b)使用
g:javascript
标记。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.