1.首先在pom文件中引入模板引擎jar包,即:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.在application.properties中配置模板引擎
spring.thymeleaf.prefix=classpath:/templates/
3.在templates下建立index.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
<h1>HelloWorld</h1>
</body>
</html>
4.controller层
@Controller
public class HelloController {
@RequestMapping(value = "/demo")
public String findOneCity(ModelMap map) {
return "/index";
}
}
注意:@controller不是@RestController,使用@RestController会返回“index”字符串