Grails 3.0 如何在 GSP 中使用 Javascript?

Grails 3.0 How to use Javascript in GSP's?

文档说,<r:script> is the right way to do this, but this is part of the Resources Plugin, which is not available in Grails 3.0, so I think in a single GSP the right way to use Javascript is at the end of the <body> with the <g:javascript>-tag.

有更好的方法吗?

您可以在 GSP 文件的底部使用 <asset:javascript src="example.js" />

但是,SiteMesh 会在布局 GSP 中 <g:layoutBody /> 所在的位置插入正文,因此最终可能会在 <script><body> 末尾之间出现一些其他标签。创建一个单独的布局 GSP 会更干净。

此外,一定要不要application.js中包含example.js,否则特殊的js文件将默认出现在每个页面上。一种选择是为其他所有内容创建一个 assets/javascripts/public 文件夹。

grails-app/assets/javascripts/application.js

//= require_tree ./public