Skip to content

集成 Thymeleaf

ContiNew Admin 默认为前后端分离项目,后端项目仅提供数据接口服务。但如果你仅仅需要使用 Thymeleaf 模板引擎来开发一些简单的页面,那么按下方步骤操作即可。

1.首先,引入模板引擎的依赖。

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

2.在 resources/templates 目录下新增模板文件。

例如:test.html

html
<!DOCTYPE html>
<html>
<head></head>
<body>
    <h1>呵呵</h1>
</body>
</html>

注意:如果你想放在其他位置,可以在 application.yml 中指定模板文件的位置(Spring Boot 默认配置如下)。

yaml
### Thymeleaf 配置
spring:
  thymeleaf:
    prefix: classpath:/templates/
    suffix: .html
    mode: HTML

3.编写 Controller,访问模板页面。

注意:是 @Controller 而不是 @RestController,并根据需要做好认证处理。

java
@SaIgnore // 忽略认证
@Controller
public class TestController {

    @GetMapping("/test")
    public String test() {
        // 返回模板名称
        return "test";
    }
}

4.启动/重启后端项目,访问 URL http://localhost:8000/test

操作完上方步骤后即可访问到模板页面。