在Grails 3应用程序中引用Bootstrap的最佳方法

10

我想要将Twitter的Bootstrap引入我的Grails 3应用程序中。最好的方法是什么?我既不想将源代码放在版本控制下,也不想引用远程CDN。

在我的build.gradle中添加"org.grails:grails-bootstrap:3.0.1"作为编译依赖项会下载相关的JAR文件,但是如何将其连接到我的应用程序中,最终能够从我的视图/GSP中引用Bootstrap类呢?


你能展示一下如何将WebJar添加到你的项目中吗? - sphinks
你找到解决方案了吗?能否记录一下结果?有趣的是,application.css 中已经提到了 bootstrap。 - John Little
4个回答

7

Webjars 对我很有帮助,在application.jsapplication.css中使用了 webjars 链接,但链接并不总是定义良好的(请参见 typeahead 示例)

application.js:

//= require jquery-2.1.3.js
//= require_tree .
//= require_self
//= require /webjars/bootstrap/3.3.5/js/bootstrap.min
//= require /webjars/bootstrap-tagsinput/0.5/bootstrap-tagsinput
//= require /webjars/typeaheadjs/0.11.1/typeahead.bundle.js

application.css:

/*
*= require main
*= require mobile
*= require_self
*= require /webjars/bootstrap/3.3.5/css/bootstrap-theme
*= require /webjars/bootstrap/3.3.5/css/bootstrap
*= require /webjars/bootstrap-tagsinput/0.5/bootstrap-tagsinput
*/

你如何将 webjars 作为依赖进行引用?我在编译时收到错误,指出没有 '/webjars/bootstrap/3.3.5/css/bootstrap-theme' 这样的资源和其他所有链接。在 Grails 的缓存中,我看到了 webjar,但似乎应该将其放置在 Grails 项目的 'assets' 文件夹中? - sphinks
你需要在build.gradle文件中添加依赖项(请参阅webjars以查找所需的依赖项)。对于bootstrap 3.3.5,它是:compile 'org.webjars:bootstrap:3.3.5' - boraas
是的,我之前进行了注释编写。所以出现了依赖项错误。还执行了其他操作吗? - sphinks
依赖项 { ... 编译 "org.grails:grails-web-boot"编译 'org.webjars:typeaheadjs:0.11.1' 编译 'org.webjars:bootstrap:3.3.5' 编译 'org.webjars:bootstrap-tagsinput:0.5' 编译 'org.webjars.bower:bootstrap-table:1.8.1'... 运行时 "org.grails.plugins:asset-pipeline" ... // 注意:建议更新到更强大的驱动程序(Chrome、Firefox等) 测试运行时 'org.seleniumhq.selenium:selenium-htmlunit-driver:2.44.0'控制台 "org.grails:grails-console"} - boraas
我只添加了这个编译 'org.webjars:bootstrap:3.3.5',也许我应该添加一些其他的WebJar才能正常工作。我会检查的,谢谢。 - sphinks
你能否给完整的CSS引用添加像media="screen, projection"这样的属性?例如:<link rel="stylesheet" href="/assets/webjars/bootstrap/3.3.5/css/bootstrap-theme.css?compile=false" media="screen, projection"> - Jonathan Airey

2

0
你应该使用这个插件,文档会告诉你具体怎么做。
Javascript grails-app/assets/javascripts/application.js:
//= require bootstrap

样式表 grails-app/assets/javascripts/application.css:

/*
*= require bootstrap
*/

嗨,格雷格。文档引用了BuildConfig.groovy,我相信在Grails 3中已经停用了。你能指导一下如何以“新”的方式引用这个插件吗? - tomwadeson
你会把它添加到你的build.gradle文件中吗?很抱歉,我不确定。我正在避免使用Grails 3. :) 假设它仍在使用asset-pipeline,我仍然相信它适用。 - Gregg

0

我正在使用这个插件 kensiprell/bootstrap-framework,并把以下路径添加到.gitignore中,以便在第一次运行时下载资源。

/grails-app/assets/javascripts/bootstrap
/grails-app/assets/javascripts/bootstrap-all.js
/grails-app/assets/stylesheets/font-awesome
/grails-app/assets/stylesheets/bootstrap
/grails-app/assets/stylesheets/font-awesome-less.less
/grails-app/assets/stylesheets/bootstrap-less.less
/grails-app/assets/stylesheets/bootstrap-all.css
/grails-app/assets/stylesheets/font-awesome-all.css

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接