只需要两步:
1、引入对themaleaf的依赖
2、修改配置文件,添加模板路径及后缀名
代码如下:
1)pom文件中添加对themaleaf的依赖
<!-- 添加thymeleaf依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2)修改application.properties,增加如下代码
,
#thymeleaf
# 前缀
spring.thymeleaf.prefix=classpath:/templates/
# 后缀
spring.thymeleaf.suffix=.html
在/templates/目录下创建example.html文件
hello example!!!
3)创建ExampleController.java
package com.test.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.Map;
@Controller
public class ExampleController {
@RequestMapping("/example")
public ModelAndView example1() {
Map<String, String> map = new HashMap();
map.put("1", "a");
map.put("2", "b");
return new ModelAndView("example", map);
}
}
4)测试:
访问:http://localhost:8080/example