thymleaf
官网:
spring boot 文档 starter地址,当需要集成模块时:
thymeleaf是什么?
一个现代的服务器端java template 引擎。 可以处理html xml javascrip css 甚至text.
Thymeleaf is a modern server-side Java template engine for both web and standalone environments, capable of processing HTML, XML, JavaScript, CSS and even plain text.
The main goal of Thymeleaf is to provide an elegant and highly-maintainable way of creating templates. To achieve this, it builds on the concept of Natural Templates to inject its logic into template files in a way that doesn’t affect the template from being used as a design prototype. This improves communication of design and bridges the gap between design and development teams.
Thymeleaf has also been designed from the beginning with Web Standards in mind – especially HTML5 – allowing you to create fully validating templates if that is a need for you.
thymeleaf的作用?
联结前端页面和后端数据桥梁。和以前的jsp作用类似。
thymleaf怎么用?
在springboot中使用:
1.添加依赖 从springboot 官网获得
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
2. 从类ThymeleafProperties可以发现,thymeleaf的html应该放在classpath:/templates
public static final String DEFAULT_PREFIX = "classpath:/templates/";构建项目时,已自动创建该目录.
3.访问前台静态页面
templates目录下面的html,可以通过controller转发访问:
package com.wang.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HellowController {
@RequestMapping("/hello")
public String hello(){
return "Hello world!";
}
@RequestMapping("/thymeleaf")
public String helloThymeleaf(){
return "helloThymeleaf";
}
}