springboot可以集成很多模板文件来实现访问页面,今天我们主要介绍thymeleaf的集成方式,很多集成模板页面都是大同小异,也是springboot推荐使用的页面方式
一、加入依赖pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
二、控制器访问页面
package com.didispace.web;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
*
* @author e生态
* @version 1.0.0
* @blog http://blog.csdn.net/ysl_228
*
*/
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello() {
return "Hello World";
}
@RequestMapping("/")
public String index(ModelMap map) {
map.addAttribute("host", "http://blog.csdn.net/ysl_228");
map.addAttribute("test", "1234");
return "index";
}
@RequestMapping("/mvc")
public String index(Model model) {
model.addAttribute("host", "http://blog.csdn.net/ysl_228");
model.addAttribute("test", "1234");
return "mvc";
}
}
三、访问页面,默认资源访问目录resource/template/index.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>
<h1 th:text="${host}">Hello World</h1>
</body>
</html>