Backend Development 10 min read

Mastering JTE Template Engine in Spring Boot 3: Fast Rendering & Real-World Examples

This article introduces the JTE template engine for Spring Boot 3, compares its performance with other engines, provides step‑by‑step code examples for defining models, writing templates, rendering them, and integrating JTE into a Spring Boot project, along with configuration tips and advanced features such as loops, conditionals, and template inclusion.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Mastering JTE Template Engine in Spring Boot 3: Fast Rendering & Real-World Examples

Environment: Spring Boot 3.2.5

1. Introduction

Template engines separate UI from business data. Common engines include FreeMarker, Thymeleaf, and JTE, a lightweight engine with a DSL.

JTE offers very fast output, demonstrated by benchmark results:

https://github.com/casid/template-benchmark/

2. Practical Cases

2.1 Quick Start

Define a data model:

<code>public class Page {
  private String title;
  private String description;
  // getters, setters
}</code>

Define a JTE template (test.jte):

<code>@import com.pack.jte.test.Page
@param Page page
<html>
  <head>
    <meta charset="UTF-8">
    @if(page.getDescription() != null)
      <meta name="description" content="${page.getDescription()}">
    @endif
    <title>${page.getTitle()}</title>
  </head>
  <body>
    <h1>${page.getTitle()}</h1>
    <p>欢迎使用JTE模板引擎</p>
  </body>
</html></code>

Key directives:

@import imports a Java/Kotlin class.

@param Page page passes a parameter to the template.

@if … @endif creates conditional blocks.

${} outputs values.

Rendering the template

<code>CodeResolver codeResolver = new DirectoryCodeResolver(Path.of("target/classes/templates/jte"));
TemplateEngine templateEngine = TemplateEngine.create(codeResolver, ContentType.Html);
StringOutput so = new StringOutput();
templateEngine.render("test.jte", new Page("xxxooo", "这里是馍馍社交"), so);
System.err.println(so.toString());</code>

The console prints the rendered HTML.

2.2 Template Syntax

Data display uses ${} :

<code>@import my.Model
@param Model model
Hello ${model.name}!</code>

Supported types: String, Enum, primitive wrappers, and any class implementing gg.jte.Content . Note: automatic .toString() conversion is not performed for safety.

If statements

<code>@if(model.entries.isEmpty())
  I have no entries!
@elseif(model.entries.size() == 1)
  I have one entry!
@else
  I have ${model.entries.size()} entries!
@endif</code>

Pattern matching with instanceof is also supported:

<code>@if (model instanceof SubModel subModel)
  ${subModel.getSpecial()}
@endif</code>

Loops

<code>@for(Entry entry : model.entries)
  <li>${entry.title}</li>
@endfor

@for(var entry : model.entries)
  <li>${entry.title}</li>
@endfor

@for(int i = 0; i < 10; ++i)
  <li>i is ${i}</li>
@endfor</code>

Use gg.jte.support.ForSupport for loop metadata:

<code>@import gg.jte.support.ForSupport
@for(var entryLoop : ForSupport.of(model.entries))
  <tr class="${(entryLoop.getIndex() + 1) % 2 == 0 ? "even" : "odd"}">
    ${entryLoop.get()}
  </tr>
@endfor</code>

Since JTE 3.0, @else can follow a loop to render fallback content when the collection is empty.

Comments

<code><%-- This is a comment --%></code>

Comments are stripped from the final output.

Template Calls

Define a common template templates/jte/common.jte :

<code>@import com.pack.jte.test.Entry
@param Entry entry
@param boolean verbose

<h2>${entry.getTitle()}</h2>
@if(verbose)
  <h3>xxxooo</h3>
@endif</code>

Invoke it from another template:

<code>@template.common(page.getEntry(), false)</code>

2.3 Using JTE in Spring Boot

Add Maven dependencies:

<code><dependency>
  <groupId>gg.jte</groupId>
  <artifactId>jte</artifactId>
  <version>3.1.12</version>
</dependency>
<dependency>
  <groupId>gg.jte</groupId>
  <artifactId>jte-spring-boot-starter-3</artifactId>
  <version>3.1.12</version>
</dependency></code>

Configure in application.yml :

<code>gg:
  jte:
    developmentMode: true
    usePrecompiledTemplates: false
    templateLocation: target/classes/templates/jte
    templateSuffix: .jte</code>

Create a controller to render a page:

<code>@Controller
@RequestMapping("/jte")
public class TestController {

  @GetMapping("")
  public String view(Model model) {
    Page page = new Page("xxxooo", "这里是馍馍社交");
    page.setEntry(new Entry("你好中国"));
    model.addAttribute("page", page);
    return "test";
  }
}</code>

The rendered page displays the data as defined in the JTE template.

Javaperformancebackend developmentSpring BootTemplate EngineJTE
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.