Thymeleaf and htmx in Spring Boot: Live Search and One-Row Updates From Server-Rendered Fragments, With No JavaScript Framework
A task board with live search and a Mark done button, rendered by Thymeleaf and updated by htmx attributes. Typing 'emma' sends GET /tasks?q=emma and the server answers with table rows only. Clicking Mark done posts to /tasks/2/done and gets back one row, which replaces the nearest tr. One fragment serves both the first load and every update.
What you will learn
- Write a Thymeleaf template that is still plain HTML, using th:text, th:if and th:classappend
- Name fragments the server can render alone, and share one fragment between the first page load and every update
- Return a fragment from a Spring MVC controller with the fragments :: name syntax
- Wire live search and a Mark done button with hx-get, hx-post, hx-trigger, hx-target and hx-swap
Before you start
- Spring MVC controllers
- Basic HTML
A server-rendered page can still feel live. The old way reloads the whole page for every search or click, like a waiter bringing a brand new table for every plate. With htmx, only the piece that changed is swapped. This article builds a small task board in Spring Boot with Thymeleaf drawing the HTML on the server and htmx swapping in what the server sends back. You don't need to memorise every attribute, since AI can write them for you. The goal is to know what each one does, so you can judge the code.
A template is just HTML
<!-- One table row. "Mark done" posts and replaces just this row (hx-target="closest tr"). -->
<tr th:fragment="row(t)"
th:id="|task-${t.id}|"
th:classappend="${t.status == 'DONE'} ? 'done'">
<td th:text="${t.title}">Task title</td>
<td th:text="${t.owner}">Owner</td>
<td><span class="badge" th:classappend="${t.status}"
th:text="${#strings.replace(t.status, '_', ' ')}">TODO</span></td>
<td class="act">
<button th:if="${t.status != 'DONE'}" type="button"
th:attr="hx-post=@{/tasks/{id}/done(id=${t.id})}"
hx-target="closest tr" hx-swap="outerHTML">Mark done</button>
</td>
</tr>
The th: attributes fill in the blanks. th:text replaces the placeholder, so without the server the cell says "Task title". th:if draws the button only when the task is not done. th:classappend adds the done class to a finished row. Open the file in a browser and it still looks like a page.
Named pieces the server can send alone
th:fragment="row(t)" makes that row a fragment, a named piece of HTML. A second fragment loops over the tasks:
<!-- All rows for the current search: used for the first page load AND as the live-search response -->
<th:block th:fragment="searchResults">
<th:block th:each="t : ${tasks}">
<tr th:replace="~{fragments :: row(${t})}"></tr>
</th:block>
<tr th:if="${#lists.isEmpty(tasks)}"><td colspan="4" class="empty">No tasks match.</td></tr>
</th:block>
The full page uses the same piece inside the table body:
<tbody id="rows">
<th:block th:replace="~{fragments :: searchResults}"></th:block>
</tbody>
The page and the live search share one piece of HTML, so the first load and every update look exactly the same.
One page, two small answers
@Controller
public class BoardController {
private final TaskRepository repo;
public BoardController(TaskRepository repo) {
this.repo = repo;
}
/** The full page (loaded once). */
@GetMapping("/")
String page(Model model) {
model.addAttribute("tasks", repo.search(""));
return "board";
}
/** htmx live search: returns only the table rows (a fragment), not a page. */
@GetMapping("/tasks")
String search(@RequestParam(defaultValue = "") String q, Model model) {
model.addAttribute("tasks", repo.search(q));
return "fragments :: searchResults";
}
/** htmx "Mark done": returns only the one updated row. */
@PostMapping("/tasks/{id}/done")
String done(@PathVariable int id, Model model) {
Task t = repo.markDone(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
model.addAttribute("t", t);
return "fragments :: row(t=${t})";
}
}
Returning "fragments :: searchResults" tells Thymeleaf to render one named piece of fragments.html instead of a whole page. The data is a record in an in-memory list of ten tasks, so the page stays the focus:
public record Task(int id, String title, String owner, String status) {
public Task markDone() { return new Task(id, title, owner, "DONE"); }
}
TaskRepository.search lower-cases the text and keeps every task whose title or owner contains it. markDone replaces the task in the list with its done copy.
Live search
<!-- Live search: a pause in typing sends GET /tasks?q=... and swaps the rows into #rows -->
<input type="search" name="q" placeholder="Search by title or owner" autofocus autocomplete="off"
hx-get="/tasks"
hx-trigger="keyup changed delay:200ms, search"
hx-target="#rows"
hx-indicator="#rows">
hx-get says which address to call. hx-trigger says when: on a key up that changed the text, after a 200 ms pause. hx-target says where the answer goes.
The answer is table rows, not a page:
<tr id="task-1" class="done">
<td>Design the landing page</td>
<td>Emma</td>
<td><span class="badge DONE">DONE</span></td>
<td class="act">
</td>
</tr>
<tr id="task-5">
...
Mark done: swap one row
The button in the row fragment has hx-post pointing at /tasks/{id}/done, hx-target="closest tr" so the target is the nearest row, and hx-swap="outerHTML", which replaces the row itself rather than its contents. Clicking it on task 2 sends POST /tasks/2/done, and the server answers with one row:
<tr id="task-2" class="done">
<td>Build the task board screen</td>
<td>Jack</td>
<td><span class="badge DONE">DONE</span></td>
<td class="act">
</td>
</tr>
htmx replaces the nearest row with it. Before, the row had a Mark done button. After, it shows DONE with no button. One click, one tiny answer, one row changed.
One command to run it
FROM maven:3-eclipse-temurin-25 AS build
WORKDIR /app
COPY pom.xml .
RUN mvn -q -B dependency:go-offline
COPY src ./src
RUN mvn -q -B package -DskipTests
FROM eclipse-temurin:25-jre
WORKDIR /app
COPY --from=build /app/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
> docker compose up --build -d
Container s04l02-thymeleaf-htmx-board-1 Started
> docker compose logs
:: Spring Boot :: (v4.1.2)
Tomcat started on port 8080 (http) with context path '/'
Started ThymeleafHtmxApplication in 2.654 seconds (process running for 3.237)
Maven and Java 25 stay inside the containers. You only install Docker, then open localhost:8080. For full Thymeleaf pages with layouts, see Thymeleaf pages from a design.
Recap
| Piece | Job |
|---|---|
| Thymeleaf template | Draws the HTML on the server, and is still plain HTML |
th:fragment | A named piece the server can send alone |
"fragments :: row(t=${t})" | The controller returns one piece, not a page |
hx-get / hx-post | What to call |
hx-trigger | When to call it |
hx-target / hx-swap | Where the answer goes, and how it replaces what is there |
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Why Spring in 2026: What the Container Does and What Spring Framework 7 Changed
Spring in one line: a container that creates your classes and hands each one what it needs. Plus the Spring Framework 7 baseline (JDK 17 to 25, Jakarta EE 11, Tomcat 11, JUnit 6), what got better than Spring 6, and a two-line version check.
A Plain Spring Project, No Boot: Your First Bean With spring-context
One dependency, one recipe, one getBean: a Maven project with spring-context 7.0.9, a Blog record, an AppConfig with @Bean, and a main that asks the container for the Blog. Then delete @Bean and meet NoSuchBeanDefinitionException.
Inversion of Control and Dependency Injection in Spring: One Program, Built Three Ways
A PostService needs a PostRepository. Build it three ways: the service calls new itself (tight coupling), main hands it in through the constructor (DI by hand), and the Spring container does the wiring from a @Configuration recipe.
Spring Component Scanning: @Component and @ComponentScan Instead of @Bean Methods
Instead of one @Bean method per class, put @Component on EmailNotifier and PostService and @ComponentScan on an empty BlogConfig. Spring walks the package, finds the stickers and builds the beans; main doesn't change.
