SEARCH BY

All

  • All
  • Java8
  • Spring

Thymeleaf fragments for css and js files in spring boot mvc application

Post a Comment
In this article we will use blocks and fragments feature of Thymeleaf to include CSS and javascript files in Thymeleaf template files. In a template folder of Spring boot application create a folder named as fragments and then create a .html file named as css_js_header.html.
    <!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body>

<th:block th:fragment="cssOnly">
 <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/style.css">
</th:block>
</body>
</html>
In the above we have created fragment block which will be used in thymeleaf tempalte file. For example we are going to use the above block fragment in the following tempalte page
 <div th:remove="tag" th:replace="fragments/css_js_header :: cssOnly"></div>
another way is by using th:block tag
<th:block  th:replace="fragments/css_js_header :: cssOnly"></th:block>
 
jaya
I love software designing, coding, writing and teaching...

Share this article

Related Posts

Post a Comment

Place your code in <em> </em> tags to send in comments | Do not add hyper links in commnet

Close

Subscribe our newsletter for the latest articles directly into your email inbox