(一)新建一个springboot项目,添加Freemarker和ThymeLeaf依赖
dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
(二)在templates目录下新建hello.html和hello2.ftlh
hello.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>hello-thymeleaf</h1>
</body>
</html>
hello2.ftlh
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>hello2-freemarker</h1>
</body>
</html>
新建HelloController类
package org.hx.springboot_free_thyme_demo12;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@Controller
public class HelloController {
/*@PathVariable("xxx")通过@PathVariable
*可以将URL中占位符参数{xxx}绑定到处理器类的方法形参中*/
@GetMapping("/{path}")
public String index(@PathVariable String path){
return path;
}
}
默认情况下,freemarker视图优先级会高于thymeleaf ,所以视图转发不会出现问题。