{"id":167,"date":"2019-06-20T02:20:13","date_gmt":"2019-06-20T02:20:13","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/spring-boot\/?p=167"},"modified":"2019-06-20T02:20:13","modified_gmt":"2019-06-20T02:20:13","slug":"spring-boot-introduction-to-thymeleaf","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/spring-boot\/spring-boot-introduction-to-thymeleaf\/","title":{"rendered":"Spring Boot &#8211; Introduction to Thymeleaf"},"content":{"rendered":"<p>In this lesson, I would teach you how to display date in html form. We would use Thymeleaf to achieve this.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Overview of Thymeleaf<\/strong><\/p>\n<p>According to the <a href=\"https:\/\/www.thymeleaf.org\/\" target=\"_blank\" rel=\"noopener\">Thymeleaf website<\/a>, &#8220;Thymeleaf is a modern server-side Java template engine for both web and standalone environment&#8221;. Thymeleaf is capable of processing JavaScript, HTML, XML, CSS and plain text.<\/p>\n<p>Before now, we use jsp (Java Server Pages) to process dynamic content. I would not spend time on why Thymeleaf is better than jsp. But we can be sure that Thymeleaf is easier to work with and likely would completely replace jsp.<\/p>\n<p>So let&#8217;s get started!<\/p>\n<p>&nbsp;<\/p>\n<p><strong>In this Tutorial&#8230;<\/strong><\/p>\n<p>First we are going to display all our entities in form (like a list).<\/p>\n<p>Next, we would add a data entry form for each of the entities.<\/p>\n<p>Finally we would add feature for Update and Delete of records<\/p>\n<p>Further in the tutorial, we would implement a login form using Spring Security.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Add Thymeleaf and other dependencies<\/strong><\/p>\n<p>Add the following Thymeleaf dependency to your pom.xml file<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;dependency&gt;<\/span>      \r\n   <span style=\"color: #007700;\">&lt;groupId&gt;<\/span>org.springframework.boot<span style=\"color: #007700;\">&lt;\/groupId&gt;<\/span>\r\n   <span style=\"color: #007700;\">&lt;artifactId&gt;<\/span>spring-boot-starter-thymeleaf<span style=\"color: #007700;\">&lt;\/artifactId&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/dependency&gt;<\/span>\r\n\r\n<span style=\"color: #007700;\">&lt;dependency&gt;<\/span>\r\n   <span style=\"color: #007700;\">&lt;groupId&gt;<\/span>org.springframework<span style=\"color: #007700;\">&lt;\/groupId&gt;<\/span>\r\n   <span style=\"color: #007700;\">&lt;artifactId&gt;<\/span>spring-context<span style=\"color: #007700;\">&lt;\/artifactId&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/dependency&gt;<\/span>\r\n    \r\n<span style=\"color: #007700;\">&lt;dependency&gt;<\/span>\r\n  <span style=\"color: #007700;\">&lt;groupId&gt;<\/span>org.springframework<span style=\"color: #007700;\">&lt;\/groupId&gt;<\/span>\r\n  <span style=\"color: #007700;\">&lt;artifactId&gt;<\/span>spring-web<span style=\"color: #007700;\">&lt;\/artifactId&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/dependency&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Save the file so the dependencies are downloaded to the classpath<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Set up the Home Page<\/strong><\/p>\n<p>Create an index.html file inside the templates folder in the src\/main\/resources folder<\/p>\n<p>The content of the file would be as shown below:<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">&lt;!DOCTYPE html&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;html&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;head&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;meta<\/span> <span style=\"color: #0000cc;\">charset=<\/span><span style=\"background-color: #fff0f0;\">\"ISO-8859-1\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;title&gt;<\/span>Thymeleaf Tutorial<span style=\"color: #007700;\">&lt;\/title&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/head&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;body&gt;<\/span>\r\n\r\n<span style=\"color: #007700;\">&lt;h2&gt;<\/span>Welcome to Thymeleaf!<span style=\"color: #007700;\">&lt;\/h2&gt;<\/span>\r\n\r\n<span style=\"color: #007700;\">&lt;\/body&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/html&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Create a file called HomeController inside the <strong>com.kindsonthegenius.social<\/strong> package.<\/p>\n<p>Annotate the class with @Controller directive<\/p>\n<p>Write a method inside this file to return the index page you created.<\/p>\n<p>The content of the HomeController file would be as shown below.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Controller<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">HomeController<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\r\n\t<span style=\"color: #555555; font-weight: bold;\">@RequestMapping<\/span><span style=\"color: #333333;\">(<\/span>value<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">\"\/home\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> String <span style=\"color: #0066bb; font-weight: bold;\">welcome<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"background-color: #fff0f0;\">\"index\"<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span>\t\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Save the file and launch the application<\/p>\n<p>Visit http:\/\/localhost:8080 and confirm that the page displays\u00a0 as shown below.<\/p>\n<p><a href=\"https:\/\/kindsonthegenius.com\/spring-boot\/wp-content\/uploads\/2019\/06\/Thymeleaf.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-168 \" src=\"https:\/\/kindsonthegenius.com\/spring-boot\/wp-content\/uploads\/2019\/06\/Thymeleaf.jpg\" alt=\"Thymeleaf\" width=\"467\" height=\"230\" srcset=\"https:\/\/kindsonthegenius.com\/spring-boot\/wp-content\/uploads\/2019\/06\/Thymeleaf.jpg 770w, https:\/\/kindsonthegenius.com\/spring-boot\/wp-content\/uploads\/2019\/06\/Thymeleaf-300x148.jpg 300w, https:\/\/kindsonthegenius.com\/spring-boot\/wp-content\/uploads\/2019\/06\/Thymeleaf-768x378.jpg 768w\" sizes=\"auto, (max-width: 467px) 100vw, 467px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Add a navigation bar. This you do by modifying the index.html file as shown below. Just add the code below in the &lt;body&gt; &lt;\/body&gt; section<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;div<\/span> <span style=\"color: #0000cc;\">align=<\/span><span style=\"background-color: #fff0f0;\">\"center\"<\/span><span style=\"color: #007700;\">&gt;<\/span> \r\n\r\n<span style=\"color: #007700;\">&lt;a<\/span> <span style=\"color: #0000cc;\">th:href=<\/span><span style=\"background-color: #fff0f0;\">\"@{posts}\"<\/span><span style=\"color: #007700;\">&gt;<\/span>Posts<span style=\"color: #007700;\">&lt;\/a&gt;<\/span> | \r\n<span style=\"color: #007700;\">&lt;a<\/span> <span style=\"color: #0000cc;\">th:href=<\/span><span style=\"background-color: #fff0f0;\">\"@{students}\"<\/span><span style=\"color: #007700;\">&gt;<\/span>Students<span style=\"color: #007700;\">&lt;\/a&gt;<\/span> | \r\n<span style=\"color: #007700;\">&lt;a<\/span> <span style=\"color: #0000cc;\">th:href=<\/span><span style=\"background-color: #fff0f0;\">\"@{users}\"<\/span><span style=\"color: #007700;\">&gt;<\/span>Users<span style=\"color: #007700;\">&lt;\/a&gt;<\/span>Users | \r\n<span style=\"color: #007700;\">&lt;a<\/span> <span style=\"color: #0000cc;\">th:href=<\/span><span style=\"background-color: #fff0f0;\">\"@{locations}\"<\/span><span style=\"color: #007700;\">&gt;<\/span>Locations<span style=\"color: #007700;\">&lt;\/a&gt;<\/span> \r\n\r\n<span style=\"color: #007700;\">&lt;\/div&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Now, create four more html pages. Name them posts.html, students.html, users.html and location.html.<\/p>\n<p>Next, Copy across the content of index.html file into these pages.<\/p>\n<p>&nbsp;<\/p>\n<p>We would start by working on the LocationController file. We would make the controller send a list of locations to the locations.html file.<\/p>\n<p>Open the LocationController file. Change the @RequestController annotation to @Controller<\/p>\n<p>Modify the getAllLocations function to the one below:<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"> <span style=\"color: #555555; font-weight: bold;\">@RequestMapping<\/span><span style=\"color: #333333;\">(<\/span>value <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"\/locations\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n <span style=\"color: #008800; font-weight: bold;\">public<\/span> String <span style=\"color: #0066bb; font-weight: bold;\">getAllLocations<\/span><span style=\"color: #333333;\">(<\/span>Model model<span style=\"color: #333333;\">)<\/span> \r\n <span style=\"color: #333333;\">{<\/span>\t\t\t\t\r\n\tmodel<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">addAttribute<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"locations\"<\/span><span style=\"color: #333333;\">,<\/span> locationService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getAllLocations<\/span><span style=\"color: #333333;\">());<\/span>\t\t\r\n\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"background-color: #fff0f0;\">\"locations\"<\/span><span style=\"color: #333333;\">;<\/span>\t\t\r\n <span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Finally, add the code below to the locations.html file.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;ul&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;li<\/span> <span style=\"color: #0000cc;\">th:each=<\/span><span style=\"background-color: #fff0f0;\">\"location : ${locations}\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n        <span style=\"color: #007700;\">&lt;span<\/span> <span style=\"color: #0000cc;\">th:text=<\/span><span style=\"background-color: #fff0f0;\">\"${location.name}\"<\/span><span style=\"color: #007700;\">&gt;<\/span>Default<span style=\"color: #007700;\">&lt;\/span&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;\/li&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/ul&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Launch the application and visit http:\/\/localhost:8080.<\/p>\n<p>Navigate to Locations and notice that you have list of locations displayed as a bulleted list.<\/p>\n<p>If you have challenges, I would recommend you watch the video.<\/p>\n<p>In the next lesson, we would see how work on other entities but I try to do it yourself first.<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>In this lesson, I would teach you how to display date in html form. We would use Thymeleaf to achieve this. &nbsp; Overview of Thymeleaf &hellip; <!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":169,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[13],"class_list":["post-167","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spring-boot-tutorials","tag-thymeleaf"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/comments?post=167"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/167\/revisions"}],"predecessor-version":[{"id":170,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/167\/revisions\/170"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/media\/169"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/media?parent=167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/categories?post=167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/tags?post=167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}