springboot整合thymeleaf模板引擎
1.pom文件引入依赖
<!-- thymeleaf模板引擎--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2.配置文件application.yml添加属性
spring: # thymeleaf模板引擎 thymeleaf: # 模板引擎 thymeleaf: mode: HTML encoding: utf-8 # 禁用缓存 cache: false
3.resources目录下添加文件夹templates,新建index.html文件
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> 名字:<span th:text="${name}"></span><br> </body> </html>
4.添加controller文件
package com.zbbz.web.controller; import com.zbbz.common.core.domain.AjaxResult; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Date; /** * @author danyu * @date 2020/01/01 */ //@RestController @Controller @RequestMapping("/web") public class WebController { @RequestMapping("/index") public String hello(ModelMap mmap) { mmap.put("name","张三"); return "index"; } }
5.浏览器访问:http://localhost:8080/zbbzfw/web/index
至此,大功告成
注意事项:
controller类如果用到@RestController注解,需改为改成@Controller,@RestController会把所有接口以json形式返回。