<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Yoseph Tenaw</title>
    <description>The latest articles on DEV Community by Yoseph Tenaw (@yosephtenaw).</description>
    <link>https://dev.to/yosephtenaw</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1280128%2Fddbe5a0e-8e6c-495c-902a-51291a549bda.png</url>
      <title>DEV Community: Yoseph Tenaw</title>
      <link>https://dev.to/yosephtenaw</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yosephtenaw"/>
    <language>en</language>
    <item>
      <title>Use HTMX with Spring Boot and Thymeleaf</title>
      <dc:creator>Yoseph Tenaw</dc:creator>
      <pubDate>Sat, 06 Apr 2024 05:41:53 +0000</pubDate>
      <link>https://dev.to/yosephtenaw/use-htmx-with-spring-boot-and-thymeleaf-18g9</link>
      <guid>https://dev.to/yosephtenaw/use-htmx-with-spring-boot-and-thymeleaf-18g9</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu8stqk7vbzxnu4rcf0ci.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu8stqk7vbzxnu4rcf0ci.jpg" alt="Image description" width="607" height="499"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, you want to create a front-end for your app, and you're gearing up for the next step. These days, it most likely means considering one of the three horsemen of JavaScript frameworks: React, Vue, and Angular... and perhaps Svelte (which I've heard is pretty alright). If just the thought of this phase of development exhausts you, you're likely suffering from JavaScript fatigue. This is when everywhere you turn, it's JS! and you're just bored or tired of it. What makes it even more exhausting is when you have to use a JS framework to create a simple admin page that could have just been an SQL query, to be honest.&lt;/p&gt;

&lt;p&gt;If you're anything like me, you'd probably try to sidestep this situation by using server-rendered pages and just hurling HTML at the browser on every request. Even though this might seem a bit crude, I'm okay with it, but management might not appreciate the lack of some interactivity. So... is there a way to do most of the rendering on the server and add that little bit of reactivity to the page without succumbing to the Deception of React, the Temptation of Vue, and the Biblical Leviathan that is Angular?"&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"Come to me, all you who labor under the weight of JavaScript, and I will give you rest. For my use case is easy, and my burden is light." - HTMX&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In this blog we will be making a full-stack app (without DB) using HTMX, server side templating engine in Thymeleaf and Spring Framework for the backend. I hope you enjoy it. Full code can be found &lt;a href="https://github.com/JosephVoid/htmx-with-spring-boot-and-thymleaf"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is HTMX
&lt;/h2&gt;

&lt;p&gt;From the official &lt;a href="https://htmx.org/docs/#introduction"&gt;site&lt;/a&gt;, "Htmx is a library that allows you to access modern browser features directly from HTML, rather than using javascript" &lt;br&gt;
So basically HTMX is JS library (How ironic), that enhances your page to access things HTTP requests and events directly from HTML tags. You can use these handles to initiate actions and react to events without a single code of JavaScript. Pretty cool. You can read more about this on their documentation page &lt;a href="https://htmx.org/docs/#introduction"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  What are Spring Boot &amp;amp; Thymeleaf
&lt;/h2&gt;

&lt;p&gt;Spring Boot is a Java-based framework that provides a streamlined approach to setting up and configuring Spring-based applications, reducing the burden of manual setup and allowing developers to focus on writing business logic rather than dealing with infrastructure concerns.&lt;/p&gt;

&lt;p&gt;Thymeleaf, on the other hand, is a modern server-side Java template engine that enables developers to create dynamic and interactive web pages. It seamlessly integrates with Spring Boot, allowing developers to easily generate HTML content using natural and readable syntax, while also providing powerful features for templating and data binding.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why do this? (For the haters)
&lt;/h2&gt;

&lt;p&gt;In an era where Java seems to fall out of favor with many developers and JavaScript frameworks overwhelm us daily, undertaking this endeavor is akin to a rebellion against the dominance of frameworks and a light jab at those who dismiss Java&lt;/p&gt;
&lt;h2&gt;
  
  
  How to do this
&lt;/h2&gt;

&lt;p&gt;Now that we got all the concepts and biases out of the way, lets get to implementing. You can follow along with the below steps or just go straight to the repo here. The first step is:  &lt;/p&gt;
&lt;h4&gt;
  
  
  Setup
&lt;/h4&gt;

&lt;p&gt;As with every spring boot app it's easier to go to the spring initializer site (&lt;a href="https://start.spring.io/"&gt;https://start.spring.io/&lt;/a&gt;) as select the starter dependencies and other configs. For this project select the following dependencies &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Thymleaf (this is our templating engine, renders our HTML)&lt;/li&gt;
&lt;li&gt;Spring Web (this is essential to build web applications)&lt;/li&gt;
&lt;li&gt;Spring Boot Dev tools (For live reloads, this is optional)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since I want this blog to be focued on the front-end part of the application you can implement/see the controllers and configurations your self by going to this repository&lt;/p&gt;
&lt;h4&gt;
  
  
  Create HTML Pages
&lt;/h4&gt;

&lt;p&gt;In the previous step we have laid out the functions/endpoints that our web app serves. Now lets see what it serves. Spring boot, with default configuration, responds with &lt;code&gt;application/json&lt;/code&gt; response type. But what we want is for it respond to our request with HTML, so to help us with this, we have added Thymeleaf in our dependencies list. When adding Thymeleaf it auto-configures Spring Boot to respond with HTML files and fragments inside our &lt;code&gt;resources/templates&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Lets create the HTML files&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resource/templates
-- dashboard.html
-- login.html
-- fragments/
---- core.html
---- dialog.html
---- head.html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;N.B: Fragments are html snippets that can be returned by the server. Think an isolated &lt;code&gt;&amp;lt;div th:fragment='frag'&amp;gt;..&amp;lt;/div&amp;gt;&lt;/code&gt; this is a fragments named &lt;code&gt;frag&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now lets build our HTML pages and fragments with HTMX included&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//head.html
&amp;lt;head th:fragment="head"&amp;gt;
  &amp;lt;meta charset="UTF-8" /&amp;gt;
  &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0" /&amp;gt;
  &amp;lt;title&amp;gt;HTMX Admin Panel&amp;lt;/title&amp;gt;
  &amp;lt;link
    rel="stylesheet"
    href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css"
    integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN"
    crossorigin="anonymous"
  /&amp;gt;
  &amp;lt;script
    src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.min.js"
    integrity="sha384-BBtl+eGJRgqQAUMxJ7pMwbEyER4l1g+O15P+16Ep7Q9Q+zqX6gSbd85u4mG4QzX+"
    crossorigin="anonymous"
  &amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;script
    src="https://unpkg.com/htmx.org@1.9.11"
    integrity="sha384-0gxUXCCR8yv9FM2b+U3FDbsKthCI66oH5IA9fHppQq9DDMHuMauqq1ZHBpJxQ0J0"
    crossorigin="anonymous"
  &amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;style&amp;gt;
    .loginform {
      display: flex;
      flex-direction: column;
      padding: 1em;
    }
    .form-cont {
      width: 50%;
    }
    .loginbody {
      padding: 4em;
    }
  &amp;lt;/style&amp;gt;
&amp;lt;/head&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our first fragment is the &lt;code&gt;head&lt;/code&gt; fragment found in the head.html&lt;br&gt;
It loads up our bootstrap CSS and JS files, and most importantly the HTMX library which will allow to do some cool stuff. This fragment will be included in our main &lt;code&gt;dashboard.html&lt;/code&gt; and &lt;code&gt;login.html&lt;/code&gt; files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// login.html

&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html xmlns:th="http://www.thymeleaf.org"&amp;gt;
  &amp;lt;head th:replace="~{fragments/head ::head}"&amp;gt;&amp;lt;/head&amp;gt;
  &amp;lt;body class="loginbody"&amp;gt;
    &amp;lt;div class="m-auto form-cont"&amp;gt;
      &amp;lt;h1&amp;gt;Login&amp;lt;/h1&amp;gt;
      &amp;lt;form action="/login" method="post" class="loginform"&amp;gt;
        &amp;lt;input
          type="text"
          name="username"
          id="username"
          placeholder="Username"
          class="mb-2"
        /&amp;gt;
        &amp;lt;input
          type="password"
          name="password"
          id="pass"
          placeholder="Password"
          class="mb-2"
        /&amp;gt;
        &amp;lt;button class="btn btn-primary" type="submit"&amp;gt;Login&amp;lt;/button&amp;gt;
      &amp;lt;/form&amp;gt;
    &amp;lt;/div&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This a regular HTML being rendered by Thymeleaf, we used the attribute &lt;code&gt;th:replace&lt;/code&gt; to import a fragment and apply it in this html. This page is the login page, it send login details to the &lt;code&gt;/login&lt;/code&gt; endpoint.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//dashboard.html
&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
  &amp;lt;head th:replace="~{fragments/head ::head}"&amp;gt;&amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;div class="container"&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;h1 class="mb-4 mt-4"&amp;gt;Dashboard&amp;lt;/h1&amp;gt;
        &amp;lt;button class="btn btn-sm btn-info" hx-post="/logout"&amp;gt;Log Out&amp;lt;/button&amp;gt;
        &amp;lt;div class="d-flex justify-content-between"&amp;gt;
          &amp;lt;h2 class="mb-4"&amp;gt;People&amp;lt;/h2&amp;gt;
          &amp;lt;button
            class="btn btn-primary btn-xs"
            hx-get="/refresh"
            hx-target="table"
          &amp;gt;
            Refresh
          &amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;table class="table table-striped" style="table-layout: fixed"&amp;gt;
          &amp;lt;thead&amp;gt;
            &amp;lt;td&amp;gt;Id&amp;lt;/td&amp;gt;
            &amp;lt;td&amp;gt;Name&amp;lt;/td&amp;gt;
            &amp;lt;td&amp;gt;Description&amp;lt;/td&amp;gt;
            &amp;lt;td&amp;gt;Action&amp;lt;/td&amp;gt;
          &amp;lt;/thead&amp;gt;
          &amp;lt;tbody id="main_table"&amp;gt;
            &amp;lt;tr th:id="'id_' + ${row.id}" th:each="row : ${rows}"&amp;gt;
              &amp;lt;td th:text="${row.id}"&amp;gt;&amp;lt;/td&amp;gt;
              &amp;lt;td th:text="${row.name}"&amp;gt;&amp;lt;/td&amp;gt;
              &amp;lt;td th:text="${row.desc}"&amp;gt;&amp;lt;/td&amp;gt;
              &amp;lt;td&amp;gt;
                &amp;lt;button
                  class="btn btn-danger"
                  th:hx-delete="'/delete?id=' + ${row.id}"
                  th:hx-target="'#id_' + ${row.id}"
                &amp;gt;
                  Delete
                &amp;lt;/button&amp;gt;
                &amp;lt;button
                  class="btn btn-primary"
                  data-bs-toggle="modal"
                  data-bs-target="#editModal"
                  th:hx-get="'/edit-form?id=' + ${row.id} +'&amp;amp;name='+ ${row.name} +'&amp;amp;desc='+ ${row.desc}"
                  hx-target="#edit-dialog-body"
                  hx-trigger="click"
                &amp;gt;
                  Edit
                &amp;lt;/button&amp;gt;
              &amp;lt;/td&amp;gt;
            &amp;lt;/tr&amp;gt;
          &amp;lt;/tbody&amp;gt;
        &amp;lt;/table&amp;gt;
        &amp;lt;button
          class="btn btn-primary"
          data-bs-toggle="modal"
          data-bs-target="#createModal"
        &amp;gt;
          Add New
        &amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;div th:replace="~{fragments/dialog :: createmodal}"&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;div th:replace="~{fragments/dialog :: editmodal}"&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is our dashboard page, it is first rendered by Thymeleaf. &lt;br&gt;
The &lt;code&gt;th:each="row : ${rows}"&lt;/code&gt; attribute helps iteratively render HTML elements, in this case it loops over the rows variable passed from Spring boot and replacing the contents of children with &lt;code&gt;th:text&lt;/code&gt; property.&lt;/p&gt;

&lt;p&gt;N.B: If an attribute has a &lt;code&gt;th:&lt;/code&gt; attached in front of any html attribute, it will process the &lt;code&gt;${}&lt;/code&gt; inside the attribute. For example &lt;code&gt;&amp;lt;td th:text="${row.desc}"&amp;gt;&amp;lt;/td&amp;gt;&lt;/code&gt; when return from the server &lt;code&gt;row.desc&lt;/code&gt; will be replaced/processed by thymeleaf into an actual value.&lt;/p&gt;

&lt;p&gt;With this in mind lets talk about the htmx attributes. In this page we use: &lt;code&gt;hx-get&lt;/code&gt; &lt;code&gt;hx-delete&lt;/code&gt; and &lt;code&gt;hx-target&lt;/code&gt;&lt;br&gt;
we used &lt;code&gt;hx-get&lt;/code&gt; in the line&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;button
  class="btn btn-primary"
  data-bs-toggle="modal"
  data-bs-target="#editModal"
  th:hx-get="'/edit-form?id=' + ${row.id} +'&amp;amp;name='+ ${row.name} +'&amp;amp;desc='+ ${row.desc}"
  hx-target="#edit-dialog-body"
  hx-trigger="click"
 &amp;gt;Edit&amp;lt;/button&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After render, you can ignore the &lt;code&gt;th:&lt;/code&gt; attribute and replace the ${} with actual values. What this htmx attribute will do is send a GET request to &lt;code&gt;/edit-form&lt;/code&gt; endpoint and replace an HTML element that has an id of &lt;code&gt;edit-dialog-body&lt;/code&gt;(found in another fragment) with the returned html value. And this all is triggered by an click on the button.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button
 class="btn btn-danger"
 th:hx-delete="'/delete?id=' + ${row.id}"
 th:hx-target="'#id_' + ${row.id}"
&amp;gt;
 Delete
&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When this button is clicked, it will send a DELETE request to the &lt;code&gt;/delete&lt;/code&gt; endpoint and it will remove an html element that has an id specified in the hx-target attribute&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        &amp;lt;form
          hx-post="/add"
          hx-target="#main_table"
          hx-swap="beforeend"
          class="d-flex flex-column"
        &amp;gt;
          &amp;lt;input type="text" name="id" placeholder="Id" class="mb-2" /&amp;gt;
          &amp;lt;input type="text" name="name" placeholder="Name" class="mb-2" /&amp;gt;
          &amp;lt;input
            type="text"
            name="desc"
            placeholder="Description"
            class="mb-2"
          /&amp;gt;
          &amp;lt;button
            type="submit"
            class="btn btn-sm btn-primary"
            data-bs-dismiss="modal"
          &amp;gt;
            Add
          &amp;lt;/button&amp;gt;
        &amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the &lt;code&gt;createmodal&lt;/code&gt; fragment we have a form that is used to create an entry. It sends a POST request to /add endpoint when the form is submitted. When it receives a response to will grab the table (#main_table) and it appends the responded HTML (which is a row fragment); this is why we used &lt;code&gt;beforeEnd&lt;/code&gt; on hx-swap attribute&lt;/p&gt;

&lt;p&gt;These are the main things about htmx mostly. You can take a look at the repository for the full code and more explanations&lt;/p&gt;

&lt;h4&gt;
  
  
  Testing it out
&lt;/h4&gt;

&lt;p&gt;This is what it is supposed to look like after its done&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnxb0e7ymb3qar3q8udrp.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnxb0e7ymb3qar3q8udrp.gif" alt="Finished" width="800" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  My Two Cents
&lt;/h4&gt;

&lt;p&gt;Alright, now let's get down to the facts and caveats. While I appreciate HTMX and the simplicity it brings, there are certain tasks that are considerably more challenging to accomplish with it. In those cases, you're better off sticking with an established framework. Every website is not a simple informational screen - most of the time it requires heavy interactivity. However, if you find that a full-fledged JS framework is overkill for your basic internal site, for instance, I strongly encourage you to give HTMX a try.&lt;/p&gt;

&lt;p&gt;Stay safe guys&lt;br&gt;
Yoseph Tenaw&lt;/p&gt;

</description>
      <category>springboot</category>
      <category>htmx</category>
      <category>thymeleaf</category>
    </item>
  </channel>
</rss>
