Using Twitter Bootstrap in Spring Boot

View more categories:

1- Objective of post

Before starting the lesson, let's take a few minutes to understand Bootstrap:

The Bootstrap is a HTML, CSS & JavaScript Framework allowing designing and developing the "Responsive Web Mobile" applications. The Bootstrap includes HTML templates, CSS templates & Javascript and basis and available things such as: typography, forms, buttons, tables, navigation, modals, image carousels and others. The Bootstrap also has Javascript Plugins, which helps the designing of your Web Reponsive to be more easier and quicker.

The Bootstrap is developed by the  Mark Otto and  Jacob Thornton at  Twitter. It was published as an open resource code in August  2011 on  GitHub.
See more:
  • TODO Link!
In this post, I am going to show you how to create a Spring Boot application using the Bootstrap. And because the Bootstrap is a Html, Css, Javascript Framework , you use any technology for the View layer (JSP, Thymeleaf, Freemarker, ...).
The contents to be mentioned in this lesson:
  1. Create a Spring Boot application.
  2. Declare necessary libraries so that you can use the Bootstrap.
  3. Create a simple page using UI Components provided by the Bootstrap.

2- Create a Spring Boot project

3- Declare pom.xml

The Bootstrap has been packaged into a small, beautiful  Jar file. To use it, you only need to declare it in the  pom.xml, and so you are ready to work with the Bootstrap Framework.
** Bootstrap **
<!-- -->
The full contents of the  pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""


    <description>Spring Boot + Bootstrap</description>

        <relativePath/> <!-- lookup parent from repository -->


        <!-- -->




4- Resource Handler

The data resources of the  Bootstrap are packaged in the  Jar file. You need to expose them so that they can be accessed by paths, for example:
  • http://somedomain/SomeContextPath/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css
  • http://somedomain/SomeContextPath/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js
package org.o7planning.sbbootstrap.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

public class WebMvcConfig implements WebMvcConfigurer {

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // Access Bootstrap static resource:
        // http://somedomain/SomeContextPath/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css
        registry.addResourceHandler("/webjars/**") //


5- Controller
package org.o7planning.sbbootstrap.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

public class HelloWorldController {

    public String helloWorld(Model model) {
        return "index";


6- View

<!DOCTYPE html>
<html xmlns:th="">
      <meta charset="UTF-8"/>
      <title>Twitter Bootstrap Example</title>
      <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
      <!-- Bootstrap. @see version in pom.xml -->
      <link th:href="@{/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css}"
                rel="stylesheet" media="screen"/>
      <script src=""></script>
      <script th:src="@{/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js}"></script>   
      <h2>Hello Twitter Bootstrap</h2>
      <div class="btn-group">
            <button type="button" class="btn btn-success">This is a success button</button>
            <button type="button" class="btn btn-warning">This is a warning button</button>
            <button type="button" class="btn btn-danger">This is a danger button</button>
And this is the image of website with the UI Components of the  Bootstrap:

View more categories: