SpringBoot template engine configuration Thymeleaf

Thymeleaf template engine

What is a template engine

Template engine (here especially for Web development template engine) is to make the user interface and business data (content) resulting from the separation, it can generate a document in a specific format, for a website template engine will generate a standard HTML document.


Learning Video: http://www.itlaoqi.com/chapter/1688.html

Source Address: QQ group 814,077,650, self-help group sharing download

Old Qi official website: itlaoqi.com (in which more dry)


Thymeleaf features

  • Thymeleaf advantage of
    mainstream unique front and rear end of the universal template engine, static html embed tag attributes, the browser can directly open the template file for easy front and rear ends of the FBI.
    springboot official recommendations.
  • Thymeleaf drawback
    template must meet the xml specification.
    slow!

    Qi old advice

  • Although the official Spring Boot Thymeleaf is the default, but too niche in China.
  • For Thymeleaf, focus on mastering the concept of template engine, to understand the syntax on the line.
  • Looking for work focusing on learning Freemarker, prospective study Beetl.
  • JSP Forget it, under the "Go J2EE" trend, with who silly X.

    Thymeleaf environment to build

    pom dependent on the introduction of thymeleaf

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

application.properties increase thymeleaf configuration

Do not use cache to ensure timely refresh after modify the source file

spring.thymeleaf.cache=false

Create a standard index.html file in templates in

thymeleaf template must define th namespace, others are standard HTML tags

<!DOCTYPE html>
<!--最重要的是要引入thymeleaf的命名空间 -->
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    ...

In preparation Controller test data, complete view and model binding

@Controller
public class ThymeleafController {
    @RequestMapping("/")
    public ModelAndView index(String keyword) {
        //参数值index就对应了templates/index.html
        ModelAndView mav = new ModelAndView("index");
        ...
        //将查询结果放入mav
        mav.addObject("emps" , list);
        return mav;
    }
}

index.html Use th: each set of attributes iteration emps

<tr th:each="emp,stat:${emps}" >
    <td>[[${emp.job}]]</td>
    <td>[[${#dates.format(emp.hiredate , 'yyyy年MM月dd日')}]]</td>
</tr>

Guess you like

Origin www.cnblogs.com/itlaoqi/p/11391751.html