Spring Boot | Using WebJars

What Are WebJars?

WebJars package client-side libraries such as jQuery and Bootstrap as JAR files so that you can manage them with Maven and Gradle in the same way as Java libraries.

WebJars - Web Libraries in Jars

Adding jQuery UI

Writing the Code

build.gradle

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
+   compile 'org.webjars:jquery-ui:1.11.4'
}

src/main/resources/static/index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>jQuery UI by WebJars</title>

    <link rel="stylesheet" href="/webjars/jquery-ui/1.11.4/jquery-ui.min.css" />

    <script src="/webjars/jquery/1.11.1/jquery.min.js"></script>
    <script src="/webjars/jquery-ui/1.11.4/jquery-ui.min.js"></script>
    <script>
    $(function() {
        $('button')
          .button()
          .on('click', function() {
              alert('Hello WebJars!!');
          });
    });
    </script>
  </head>
  <body>
    <button>Hello</button>
  </body>
</html>

Result

Start the server and open http://localhost:8080/ in a browser.

Explanation

  • Libraries added through WebJars are accessible under the webjars/ path.
  • You can inspect the folder structure inside the JAR file or click the Files link on WebJars.