Skip to content

tucanoo/spring-boot-htmx-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HTMX + Spring Boot Tutorial — Contact Manager

A companion project for the Tucanoo article "Building the Modern JVM Monolith: A Production-Grade Guide to HTMX with Spring Boot".

This is a simple Contact Manager that demonstrates every major HTMX + Spring Boot pattern covered in the article. It deploys as a single JAR with zero npm dependencies.

Prerequisites

  • Java 21+ (check with java -version)

That's it. The Gradle wrapper is included — no separate Gradle installation needed.

Quick Start

git clone https://github.com/tucanoo/spring-boot-htmx-tutorial.git
cd spring-boot-htmx-tutorial
./gradlew bootRun

Open http://localhost:8080 and log in with:

  • Username: user
  • Password: password

What This Project Demonstrates

Each feature maps directly to a section in the article:

Feature Article Section Where to Look
@HxRequest — fragment vs full-page rendering The Mental Model ContactController.listContacts() / listContactsFragment()
Out-of-Band swaps (FragmentsRendering) OOB Swaps: The Killer Feature ContactController.deleteContact() — updates table AND count
CSRF token handling Spring Security: CSRF layout/main.htmlhx: dialect auto-injects; meta tags as fallback
Session expiry (HxRefreshHeaderAuthenticationEntryPoint) Handling Authentication Failures SecurityConfig.java
Validation error fragments Error Handling: Return Error Fragments ContactController.createContact()
Server error routing (response-targets) Error Handling: Response Targets Extension layout/main.html + HtmxErrorHandler.java
htmx.process() for dynamic DOM content The htmx.process() Gotcha contacts/list.html — dynamic content demo
Loading indicators Loading Indicators layout/main.html (global bar) + search spinner
Search debouncing (delay:300ms, changed) Search Debouncing contacts/list.html search input
hx-boost progressive enhancement Progressive Enhancement contacts/list.html<body hx-boost="true">
hx-push-url browser history History and the Back Button Contact name links in contacts/list.html

Tech Stack

  • Spring Boot 4.0.3
  • Thymeleaf (native fragment includes for template composition)
  • htmx-spring-boot-thymeleaf 5.0.0 (provides @HxRequest, hx: dialect, security helpers)
  • HTMX 2.0.8 (14 KB, via CDN)
  • H2 in-memory database (data resets on restart)
  • Spring Security with form login

Project Structure

src/main/java/com/tucanoo/htmxtutorial/
├── HtmxTutorialApplication.java     # Entry point + sample data seeder
├── config/
│   ├── SecurityConfig.java          # Security + CSRF + HxRefresh entry point
│   └── WebConfig.java               # View controllers (login, root redirect)
├── contact/
│   ├── Contact.java                 # JPA entity
│   ├── ContactRepository.java       # Spring Data JPA + search query
│   ├── ContactForm.java             # Form DTO with Bean Validation
│   └── ContactController.java       # All HTMX patterns — start here
└── error/
    └── HtmxErrorHandler.java        # Global error → HTML fragment

src/main/resources/
├── application.properties
├── templates/
│   ├── layout/main.html             # Base layout (HTMX, hx-boost, CSRF, progress bar)
│   ├── login.html                   # Login page
│   ├── contacts/list.html           # Main page — all HTMX interactions here
│   └── fragments/error-banner.html  # Error state fragment
└── static/css/styles.css            # Minimal styling + HTMX CSS classes

Key Files to Read First

  1. ContactController.java — Every method is annotated with the article concept it demonstrates
  2. contacts/list.html — All HTMX attributes in action, heavily commented
  3. layout/main.html — Reusable fragments: CSRF meta tags, nav, loading indicator scripts
  4. contacts/list.html <body> tag — Global setup: hx-boost, response-targets
  5. SecurityConfig.java — CSRF + HxRefreshHeaderAuthenticationEntryPoint

About

Production-grade Contact Manager tutorial demonstrating HTMX Thymeleaf Spring Boot patterns: fragment rendering, out-of-band swaps, live search with debouncing, form validation, and Spring Security integration. Deploys as a single JAR with zero npm dependencies. Companion project for Tucanoo's "Building the Modern JVM Monolith" guide.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors