1.springboot集成Thymeleaf模版技术:
(1)在application.properties中配置Thymeleaf依赖
<!-- springboot整合Thymeleaf视图层模版引擎 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
(2)新建Controller,增加返回ModelAndView的方法:
package com.steno.propertiestest.controller;
import com.steno.propertiestest.vo.Book;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.Map;
/**
* 测试Thymeleaf java模版引起的嵌入
*/
@Controller
public class ThymeleafController {
/**
* 返回Thymeleaf模版页面
* @return
*/
@GetMapping("/getThymeleafPage")
public ModelAndView getThymeleafPage(){
Book book = new Book();
book.setName("thymeleaf");
book.setAuthor("spring");
book.setPrice("4.0");
Map<String, Object> map = new HashMap<String, Object>();
map.put("book", book);
return new ModelAndView("thymeleafPage", map);
}
}
(3)在src/main/resources/下新建templates文件夹,在该文件夹下新建.html结尾的页面
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>springboot-thymeleaf</title>
</head>
<body>
<!-- 返回值获取 -->
<p th:text="'hello, ' + ${book.author} + '的' + ${book.name} + '!'" />
</body>
</html>
(4)访问域名http://localhost:8081/project/getThymeleafPage