Last Updated on 08 July 2021   |   Print Email
In this article, I’d like to share with you 3 ways for adding Bootstrap and jQuery to a Spring Boot project – with pros and cons of each way.You know, jQuery is a popular Javascript library and Bootstrap is a popular CSS library – both are the de factor standard client-side technologies for web development. And Spring is a popular framework that is used for developing enterprise Java applications, with Spring Boot is a set of tools and libraries that make Spring development a lot easier.So using Bootstrap and jQuery in a Spring Boot project is developer’s common choice.
1. Add Bootstrap and jQuery via CDN
This is the most common way for adding Bootstrap and jQuery in a web development project: refer to files hosted on a Content Delivery/Distribution Network (CDN).For jQuery, just use a script tag that refers to the official jQuery’s CDN like this (compressed version):
Notes: The integrity attribute contains a security code that ensures the file is not modified on the way from server to client; and the crossorigin=”anonymous”attribute allows the script to be loaded from a different domain.You can also use Bootstrap and jQuery files that are hosted on Google’s CDN, Microsoft’s CDN…Pros: simple and easy to setup (just add some links). Boots reliability and response times of your website as CDNs deliver the resources geographically with data centers around the globe.Cons:internet connection is always required, and you cannot modify the code of jQuery or Bootstrap files if needed.
2. Add Bootstrap and jQuery Files to the project
In this second way, you have to download jQuery and Bootstrap files on to your local computer, and put them into the static folder inside your Spring Boot project – something like this:Then in the view layer, you refer to these files in the HTML template file like this:
Here, you should use Thymeleaf’s syntax th:href and th:src so it will load the files properly.Pros: You have total control, e.g. modify the code of jQuery and Bootstrap as you wish. Can be used offline.Cons: It take times to setup (manually download the files and put them to the project). Response time maybe slow as no CDN is used. Your website hosts the files itself.
3. Add Bootstrap and jQuery via WebJars
In this way, you add jQuery and Bootstrap files by declaring some dependencies in your project’s pom.xml file like this:
Maven will download the Java JAR files that contain the files of jQuery and Bootstrap on to your computer – something like this:This is possible by using WebJars, which are client-side libraries packaged into JAR (Java Archive) files. Then load the files of jQuery and Bootstrap in a Thymeleaf template file like this:
Pros: Quick and easy to setup. No need to know where the files are. Can be used offline.Cons: Response time can be slow as no CDN is used. Your website hosts the files itself. You cannot modify the code of jQuery and Bootstrap if needed. To see the steps in action with a sample Spring Boot web application, watch the following video:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments