Summary of simple application of Thymeleaf

Summary of simple application of Thymeleaf

preface

To summarize the simple application of Thymeleaf, take Spring MVC as an example.

1, What is Thymeleaf?

Thymeleaf is a template engine for rendering XML/XHTML/HTML5 content, which can be easily integrated with Web frameworks such as Spring MVC.

2, Use steps

1. configure the view parser

The code is as follows (example):

<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="order" value="1"/>
        <property name="characterEncoding" value="UTF-8"/>
        <property name="templateEngine">
            <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
                <property name="templateResolver">
                    <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">

                        <!-- View prefix -->
                        <property name="prefix" value="/WEB-INF/templates/"/>

                        <!-- View suffix -->
                        <property name="suffix" value=".html"/>
                        <property name="templateMode" value="HTML5"/>
                        <property name="characterEncoding" value="UTF-8" />
                    </bean>
                </property>
            </bean>
        </property>
    </bean>

When the view name set in the controller method does not have any prefix, the view name at this time will be configured in the SpringMVC configuration file
The final path obtained by splicing the view prefix and view suffix with the view name will be skipped through the internal forwarding of the server. The prefix here is /WEB-INF/templates/ and the suffix is html

2. specific use of thymeleaf

(1) Add a reference to the html root tag

<html xmlns:th="http://www.thymeleaf.org">

(2) Send request with hyperlink

<a th:href="@{/target}">target</a>

Writing absolute paths can be avoided in this way
If you want to take parameters, you can use string splicing to add? key=value&…
The following methods can also be adopted

<a th:href="@{/test(username='admin',password=123456)}">test</a>

(3) Send request with form form

<form th:action="@{/test}" method="post">
    <input type="submit" value="test">
</form>

There will be a submit button, which can be pressed to send the request
The method attribute here represents the method of the request. Here, you can only use get or post. Use other methods or do not write this attribute. The default is to request in the get method
SpringMVC provides HiddenHttpMethodFilter to help us transform post requests into delete or put requests
On the web The following code should be added to the XML configuration file

<filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

A request parameter needs to be added to the form_ The method code is as follows:

<form th:action="@{/test}" method="post">
    <input type="hidden" name="_method" value="put">
    <input type="submit" value="test"><br>
</form>

At this time, the real request method is the value after value in the second line

(4) Get parameters in shared domain

Get the parameters of the request domain

<input type="text" th:value="${key}">

Get the parameters of the session domain

<input type="text" th:value="${session.key}">

Get the parameters of the application domain

<input type="text" th:value="${application.key}">

(5)if and unless usage

<tr th:if="${#lists. Isempty (session.key)} "> does not exist </tr>
<tr th:unless="${#lists. Isempty (session.key)} "> exists </tr>

This means that if the key value in the session domain is empty, it will display "does not exist". Otherwise, it will display "exists"

(6)each usage

<tr th:each="element : ${session.list}">
	<td th:text="${element.name}"></td>
	<td th:text="${element.age}"></td>
	<td th:text="${element.sex}"></td>
</tr>

This is equivalent to a for... in... Loop.
There is a list in the session domain, which contains n objects. The above code can display the properties of each object

summary

The above is the content of this blog. This article only briefly introduces Thymeleaf More detailed applications can be found online.

Tags: Java Spring mvc

Posted by stelthius on Thu, 02 Jun 2022 00:33:19 +0530