[JAVA] Introduction to Ratpack (9) --Thymeleaf

Ratpack introductory series

  1. Introduction to Ratpack (1) --What is Ratpack
  2. Introduction to Ratpack (2) --Architecture
  3. Introduction to Ratpack (3) --Hello world detailed explanation
  4. Introduction to Ratpack (4) --Routing & Static Content
  5. Introduction to Ratpack (5) --Json & Registry
  6. Introduction to Ratpack (6) --Promise
  7. Introduction to Ratpack (7) --Guice & Spring
  8. Introduction to Ratpack (8) --Session
  9. Introduction to Ratpack (9) --Thymeleaf

Thymeleaf

Ratpack provides modules that support the Thymeleaf template engine.

build.gradle


dependencies {
    compile "io.ratpack:ratpack-thymeleaf:${ratpack}"
}

Add a module.

Function<Registry, Registry> registry = ratpack.guice.Guice.registry( bindings -> {
    bindings.module( new ThymeleafModule() );
} );

By default, the Thymeleaf module recognizes * .html files under $ {BaseDir} / thymeleaf as templates. Therefore, it is necessary to set BaseDir.

ServerConfig.builder()
            .development( true )
            .findBaseDir( "public/.ratpack" )

In this case, public / thymeleaf is considered the template directory.

The module registers a Renderer for the ratpack.thymeleaf.Template class. Note that it is not a renderer ** for Thymeleaf's ʻIContext**.Template can be created from the Template.thymeleafTemplate () `method.

Action<Chain> handlers = chain -> {
    chain.get( "/:name?", ctx -> {
        String name = ctx.getPathTokens().get( "name" );
        name = name.isEmpty() ? "world" : name;
        ctx.render( Template.thymeleafTemplate( ImmutableMap.of( "name", name ), "template" ) );
    } );
};

The map of the first argument is the parameter binding and the second argument is the name of the template. If BaseDir is public / .ratpack, the actual file to be resolved will be public / thymeleaf / template.html.

The template file is exactly the same as a regular Thymeleaf.

template.html


<!DOCTYPE html>
<html lang="ja" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta content="text/html; charset=UTF-8" />
</head>

<body>
<h1 th:text="'hello, ' + ${name}"></h1>
</body>

</html>

As you can see, using Thymeleaf is very easy. The module implementation itself is easy to create, as long as you have a class that represents the template and an implementation of the renderer that wraps the template engine. I think it would be easy to support Velocity and other template engines with a similar pattern.

About Thymeleaf version

~~ The modules provided by the Ratpack official are currently (February 2018) up to Thymeleaf 2.1.5. It doesn't support version 3 (I implemented it myself because it can't be helped-Promotion). ~~ Officially supported in 1.6.0 and later. Please use that.

Recommended Posts

Introduction to Ratpack (9) --Thymeleaf
Introduction to Ratpack (8)-Session
Introduction to Ratpack (6) --Promise
Introduction to Ratpack (2)-Architecture
Introduction to Ratpack (7) --Guice & Spring
Introduction to Ratpack (1) --What is Ratpack?
Introduction to Ruby 2
Introduction to SWING
Introduction to web3j
Introduction to Micronaut 1 ~ Introduction ~
[Java] Introduction to Java
Introduction to migration
Introduction to java
Introduction to Doma
Introduction to Ratpack (Extra Edition) --Using Sentry
Introduction to Ratpack (3) --hello world detailed explanation
Introduction to Ratpack (Extra Edition) --Ratpack written in Kotlin
Introduction to JAR files
Introduction to RSpec 1. Test, RSpec
Introduction to bit operation
Introduction to PlayFramework 2.7 ① Overview
Introduction to Android Layout
Introduction to design patterns (introduction)
Introduction to Practical Programming
Introduction to javadoc command
Introduction to jar command
Introduction to lambda expression
Introduction to java command
Introduction to RSpec 2. RSpec setup
Introduction to Keycloak development
Introduction to javac command
[Introduction to Spring Boot] Submit a form using thymeleaf
Introduction to Design Patterns (Builder)
Introduction to RSpec 5. Controller specs
Introduction to RSpec 6. System specifications
Introduction to Android application development
Introduction to RSpec 3. Model specs
Introduction to Metabase ~ Environment Construction ~
Refer to enum in Thymeleaf
(Dot installation) Introduction to Java8_Impression
Introduction to Design Patterns (Composite)
Introduction to Micronaut 2 ~ Unit test ~
Introduction to JUnit (study memo)
Introduction to Spring Boot ① ~ DI ~
Introduction to design patterns (Flyweight)
[Java] Introduction to lambda expressions
Introduction to Spring Boot ② ~ AOP ~
Introduction to Apache Beam (2) ~ ParDo ~
[Ruby] Introduction to Ruby Error statement
Introduction to EHRbase 2-REST API
Introduction to design patterns Prototype
GitHub Actions Introduction to self-made actions
[Java] Introduction to Stream API
Introduction to Design Patterns (Iterator)
Introduction to Spring Boot Part 1
XVim2 introduction memo to Xcode12.3
Introduction to RSpec-Everyday Rails Summary-
Introduction to Design Patterns (Strategy)
[Introduction to rock-paper-scissors games] Java
Introduction to Linux Container / Docker (Part 1)
Introduction to swift practice output Chapter5