Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC with Thymeleaf + Bootstrap using webjars

I have a Spring project where I included the following webjars into pom.xml:

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>bootstrap</artifactId>
    <version>3.3.7-1</version>
</dependency>
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.1.1</version>
</dependency>

Then I included in my HTML view the following link and scripts:

<link rel="stylesheet" href="@{/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css}" />

<script src="@{/webjars/jquery/3.1.1/jquery.min.js}"></script>
<script src="@{/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js}"></script>

But it didn't work, no mapping found:

[org.springframework.web.servlet.PageNotFound] (default task-15) No mapping found for HTTP request with URI [/TestPublicWeb-0.0.1-SNAPSHOT/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css] in DispatcherServlet with name 'testapp'

...so I tried to include the following mapping into servlet.xml:

<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>

But with this, mapping for my /TestApplication is not found:

[org.springframework.web.servlet.PageNotFound] (default task-13) No mapping found for HTTP request with URI [/TestApplication/] in DispatcherServlet with name 'testapp'

How should webjars be included in a Spring project in a correct way?

like image 685
user1563721 Avatar asked Jan 09 '17 12:01

user1563721


People also ask

Can you use bootstrap with Thymeleaf?

There are two ways we can add bootstrap CSS file to Thymeleaf template: Using bootstrap CSS CDN remote links. Downloading Bootstrap CSS file locally, add to project and finally add a file path to Thymeleaf template.

What is WebJars bootstrap?

WebJars are client-side web libraries (e.g. jQuery & Bootstrap) packaged into JAR (Java Archive) files. Explicitly and easily manage the client-side dependencies in JVM-based web applications. Use JVM-based build tools (e.g. Maven, Gradle, sbt, ...) to download your client-side dependencies.

How does WebJars work?

Simply put, WebJars are client side dependencies packaged into JAR archive files. They work with most JVM containers and web frameworks. Here's a few popular WebJars: Twitter Bootstrap, jQuery, Angular JS, Chart. js etc; a full list is available on the official website.

What are web jars?

WebJars is simply taking the concept of a JAR and applying it to client-side libraries or resources. For example, the jQuery library may be packaged as a JAR and made available to your Spring MVC application. There are several benefits to using WebJars, including support for Java build tools such as Gradle and Maven.


1 Answers

The problem is that you are mixing the standard HTML href tag with Thymeleaf's syntax @{}. Change it as follows:

<link rel="stylesheet" th:href="@{/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css}" />

<script th:src="@{/webjars/jquery/3.1.1/jquery.min.js}"></script>
<script th:src="@{/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js}"></script>

And if you are using Spring Security you need also specify the authorization to your webjars in the configure(HttpSecurity http) method like:

http.authorizeRequests().antMatchers("/webjars/**").permitAll();
like image 153
DimaSan Avatar answered Sep 20 '22 16:09

DimaSan