Grails 3.3.2集成Bootstrap报错及图片不显示问题求助
Fixing Bootstrap & Asset Loading Issues After Grails 2.5.1 → 3.3.2 Upgrade
Let’s work through your problems step by step—they’re mostly tied to Grails 3’s updated asset pipeline behavior and a small configuration oversight.
1. Resolve "Bootstrap's JavaScript requires jQuery" Error
Bootstrap’s JS relies entirely on jQuery, and unlike Grails 2, Grails 3 doesn’t auto-include jQuery by default. Here’s how to fix this:
- Add jQuery to your dependencies: Ensure the jQuery plugin is in your
build.gradlefile (if it’s missing):dependencies { // ... other existing dependencies compile 'org.grails.plugins:jquery:3.4.1' // Pick a version compatible with your Bootstrap version } - Load jQuery before Bootstrap JS: In your
mainBS.gsplayout (the one referenced byindex.gsp), adjust the script order in the<head>or right before</body>:
This guarantees jQuery exists when Bootstrap’s script tries to use it.<!-- Load jQuery first to make it available globally --> <asset:javascript src="jquery.min.js"/> <!-- Then load Bootstrap's JavaScript --> <asset:javascript src="bootStrap/bootstrap.min.js"/>
2. Fix Missing Images & Asset Loading Failures
Your build.gradle assets configuration has a key mistake: you’re overwriting the includes property multiple times, so only the last entry (fonts/*) is being applied. Here’s the correction:
- Update the assets block: Combine all includes into a single list, and use
**/*to cover files in subdirectories (since your Bootstrap files live in abootStrapsubfolder):assets { minifyJs = true minifyCss = true includes = ["stylesheets/**/*", "javascripts/**/*", "images/**/*", "fonts/**/*"] } - Confirm asset directory structure: Double-check your files are in the correct
assetssubfolders:assets/javascripts/bootStrap/bootstrap.min.jsassets/stylesheets/bootStrap/bootstrap.min.cssassets/images/grails-cupsonly-logo-white.svg(so your<asset:image>tag can find it)
- Include Bootstrap CSS in your layout: Add this line to the
<head>section ofmainBS.gsp:<asset:stylesheet src="bootStrap/bootstrap.min.css"/>
3. Final Cleanup & Rebuild
After making these changes, run these commands to clear cached assets and rebuild your project:
grails clean grails compile grails run-app
This should fix both the jQuery error and missing images.
内容的提问来源于stack exchange,提问作者Sherif
相关产品推荐
相关产品推荐

