Category Archives: Thymeleaf

Thymeleaf Whitelabel Error Page – Convert html to thymeleaf template

Although , browsers are very forgiving in nature and ignore incomplete tags, but Thymeleaf blow out if it finds document to be not well structured xml document, and if we donot follow these rules, thymeleaf gonna be very angry with us, so lets understand those tweaks.

 

XML Namespace

Add xml namespace for thymeleaf

<html lang="en"> to <html lang="en" xmlns:th="http://www.thymeleaf.org">

Meta Tag

The element type “meta” must be terminated by the matching end-tag “</meta>”

<meta charset="utf-8" > to <meta charset="utf-8" />

Image Tag

Although browser ignores these, we have to do it for thymeleaf.

<img src='//imagehosting.domain.com/imageid3' > to <img src='//imagehosting.domain.com/imageid3' />

 

Link Tag

<link href='//imagehosting.domain.com/bootstrap.min.css' rel="stylesheet" > 
to 
<link href='//imagehosting.domain.com/bootstrap.min.css' rel="stylesheet"  />

 

Error : The content of elements must consist of well-formed character data or markup.

< script type="text/javascript"  >
 window.jQuery || document.write("<script src='assets/js/jquery-2.0.3.min.js'>"+"<"+"/script>");
< / script >
to
You need to add CDATA tags for the script like this
< script type="text/javascript"  >
    //<![CDATA[
     window.jQuery || document.write("<script src='assets/js/jquery-2.0.3.min.js'>"+"<"+"/script>");
    //]]>
</ script >

Eclipse Thymeleaf Intellisense Installation

In Eclipse, go to Help >> Install New Software… then either use the update site URL, or download a ZIP archive of the plugin from SourceForge:

Advertisements

Thymeleaf Hello World – day 1 – Project Structure

Developing application using Spring boot and Thymeleaf is simpler and quick, if you follow these convention. Thymeleaf requires a specific project structure which helps to render views and refer items without getting into error.

Since we are using Spring Boot, because we’ve included the dependency springbootstarterthymeleaf in our Maven POM for the project, so Spring Boot will do some things automatically for us.

Templates/Fragments

By default, Spring Boot configures the Thymeleaf template engine to read template files from src/main/resources/templates .

Effectively, you as the developer just need to start making Thymeleaf templates and dropping them into  src/main/resources/templates.

Static Resources

It includes libraries and their respective javascript (*.js) and stylesheets (*.css) files to be placed inside src/main/resources/static

 

Project Structure/Convention

  • Messages to placed inside src/main/resources/messages into the message.properties
  • It requires static web content to be placed inside src/main/resources/static (this will be the root directory for your web application for static content like images, CSS and JavaScript files)
  • Most importantly templates must go into src/main/resources/templates and if required, subdirectories there.

Be clean on configuration

  • Use src/main/resources/application.properties as few as possible
  • Use src/main/resources/applicationContext.xml as few as possible
  • Try to use annotation-based configuration as much as possible

All those points are very important, if you don’t follow them, you will get into trouble when you try to run your application standalone and not from your IDE!

Reference :

  1. http://www.christophlorenz.de/blog/learnings-with-spring-boot-and-thymeleaf/
  2. http://www.thymeleaf.org/documentation.html