springboot integrates thmeleaf template engine (a must for novices)

Posted by kvnirvana on Fri, 06 Mar 2020 06:24:34 +0100

About what tymeleaf is:

1.Thymeleaf is a popular template engine, which is developed in Java language. In addition to thmeleaf, there are template engines such as Velocity and FreeMarker, with similar functions.
2.Thymeleaf can run in the environment with and without network, that is to say, it allows artists to view the static effect of the page in the browser, and also allows programmers to view the dynamic page effect with data in the server. This is because it supports html prototypes, and then adds additional attributes to html tags to achieve template + data presentation. When the browser interprets html, it ignores the undefined tag attributes, so the template of thymeleaf can run statically; when data is returned to the page, the thymeleaf tag will dynamically replace the static content and make the page display dynamically.
3. Tymeleaf's out of the box features. It provides two dialects: standard and spring standard. You can insert code patch and apply template directly here to realize JSTL and OGNL expression effect, avoiding the trouble of setting template, changing JSTL and changing label every day. Developers can also extend and create custom dialects.

Thymeleaf integrates SpringBoot

**1. * * the first step is to add dependency in pom file and introduce thymeleaf


**2. * * in the second step, we need to configure thymeleaf in the project property file (application.yml)

        mode: HTML5
        encoding: UTF-8
        cache: false
        prefix: classpath:/templates/demo/

**3. * * the third step is to go to the controller and create a new mapping to return the url request page.

ppackage com.enlistentype.controller.demo;

import com.enlistentype.model.umodel.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

public class ThymeleafController {
    public String greetingForm(Model model) {
        model.addAttribute("user", new User());
        return "demo1";

Very simple lines of code, pass an object user to the front desk.
**3. * * in step 3, we need to create a new thmeleaf template, which should be placed in the application.yml configuration path, that is, the resource template.

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<form action="#" th:action="@{/authless/subLogin}" th:object="${user}" method="post">
    <input type="text" th:field="*{userId}" />
    <input type="text" th:field="*{password}" />
    <input type="submit" />


It's also very simple. It's mainly a form to get the login name and password. It's basically finished here. If you visit 8080/test, you will get a page with the login form. If you want to continue, you need to process the login data in a controller file, and then return to the foreground for rendering through the user object.

Published 14 original articles, won praise and 3688 visitors
Private letter follow

Topics: Thymeleaf Spring Java FreeMarker