eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

1. Overview

When working with the Thymeleaf template engine, it’s easy to unintentionally break an HTML structure while rendering dynamic content. This usually occurs when attribute-based expressions replace an element’s content, leading to incomplete output.

In this tutorial, we’ll walk through a common mistake developers make when rendering dynamic text in Thymeleaf. Also, we’ll explore two effective approaches to preserve the intended HTML structure: using text inlining and rendering dynamic content in a child element, with th:remove=”tag” used as an optional markup refinement.

2. Project Setup

To begin, let’s bootstrap a Spring Boot project by adding the spring-boot-starter-web dependency to our pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>4.0.2</version>
</dependency>

The dependency allows us to write a Spring web application with an embedded web server.

Also, let’s add the spring-boot-starter-thymeleaf dependency to our pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    <version>4.0.2</version>
</dependency>

The spring-boot-starter-thymeleaf dependency allows us to use the Thymeleaf template engine.

Finally, let’s create a controller class that handles requests to the home page and exposes the title and subtitle to the view:

@Controller
public class HomeController {
	
    @GetMapping("/home-page")
    public String homePage(Model model) {
        String title = "Introduction to Java";
        String subtitle = "[Java powers over 1 billion devices]";
		
        model.addAttribute("title", title);
        model.addAttribute("subtitle", subtitle);
		
        return "structures/home";
    }
}

In the code above, we define a controller method mapped to the /home-page endpoint. When a GET request is made, the method returns the home view and exposes the title and subtitle values as the model attributes. These attributes can then be accessed in the Thymeleaf template for rendering.

3. The Common Mistake: Breaking HTML Structure

Moving on, let’s create a home.html file in the template directory and use Thymeleaf expressions to display the title and subtitle:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    
</head>
<body>
	<h1 th:text="${title}" >
	   title
	   <small th:text="${subtitle}" >subtitle</small>
	</h1>
</body>
</html>

In the code above, we attempt to render both the title and subtitle model attributes inside an <h1> element. The subtitle is wrapped in a <small> tag to distinguish it from the main title visually.

At first glance, this approach seems perfect. However, when we compile our application and visit the URL – http://localhost:8080/home-page to see the result, we notice an unexpected result:

break html structure

In the image above, Thymeleaf only renders the title and omits the subtitle because applying th:text=”${title}” expression to the <h1> element causes Thymeleaf to remove everything inside the <h1>, including the <small> tag that contains the subtitle. Hence, only the title is rendered in the final HTML.

4. Using th:inline=’text’

To avoid the break in HTML structure, we can use Thymeleaf’s text inlining feature to render dynamic values directly inside an element’s body.

Let’s demonstrate it by rewriting the body section of our HTML code:

<body>
    <h1 th:inline="text" >
       [[${title}]]
       <small th:text="${subtitle}">subtitle</small>
    </h1>
</body>

Here, we explicitly enable text inlining by adding th:inline=”text” to the <h1> element. This allows us to use the [[ …]] inline expression syntax to insert the value of title directly into the element’s content.

Notably, text inlining is enabled by default when Thymeleaf processes templates in HTML mode. As a result, we can use the [[…]] inline expression without explicitly declaring th:inline=”text” on the element:

<body>
    <h1>
       [[${title}]]
       <small th:text="${subtitle}">subtitle</small>
    </h1>
</body>

However, explicit inlining is required when working inside script or style tags, where the processing mode must be specified.

Here’s the new rendered page:

rendering of dynamic contentwithout breaking the html structure

In the image above, both title and subtitle are rendered correctly. This works because the title is rendered using an inline expression, which preserves the surrounding HTML structure and prevents the subtitle from being omitted.

5. Preserving HTML Structure by Rendering Text in a Child Element

Alternatively, we can solve this problem by applying th:text to a child element rather than the parent <h1>  element. This approach preserves the HTML structure and prevents child elements from being removed:

<body>
    <h1>
        <span th:text="${title}" th:remove="tag">title</span>
        <small th:text="${subtitle}" >subtitle</small>
    </h1>
</body>

Here, we wrap the title in a <span> element. Since th:text replaces only the contents of the span, it doesn’t affect the child nodes of the <h1> element. As a result, the subtitle remains intact.

The  th:remove=”tag” attribute is optional and is used to remove the <span> element itself after Thymeleaf evaluates its content. This allows us to keep the final rendered HTML clean while still avoiding the original structural issue.

If we inspect the rendered HTML, we get the following output:

<body>
    <h1>
        Introduction to Java
        <small >[Java powers over 1 billion devices]</small>
    </h1>
</body>

Here, the <span> element is removed from the final output because of the th:remove=”tag” attribute. Thymeleaf evaluates the content of the <span> and then removes the element itself, leaving only its text.

If th:remove=”tag” is omitted, the rendered HTML will still include the <span> element, although the page will render correctly in both cases.

6. Conclusion

In this article, we learned the common pitfalls that can unintentionally break HTML structure when working with the Thymeleaf template engine. Also, we demonstrate the error and how to resolve it using Thymeleaf’s th:inline=”text” attribute or by rendering dynamic text in a child element, both of which allow us to render dynamic content while preserving the intended HTML markup.

As always, the complete source code for the example is available over on GitHub.

Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)
guest
0 Comments
Oldest
Newest
Inline Feedbacks
View all comments