sprignboot 中thymeleaf和freemarker 都存在时,默认选择哪个

我们 

无聊的时候想到,freemarker和thymeleaf都是springboot默认支持的模板,当这2个同时存在并有相同名字的时候,springboot会默认选择哪个模板来显示呢 ?

所以今天我就实际操作一下 ,都用默认的,不采用其他的配置来观察一下 ,。导入这2个模板的jar

<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新建一个html文件,一个ftl文件,分别为hello.html,

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>Title</title>
</head>
<body>
<h2>this is a <h2  th:text="${name}"> </h2>page</h2>
</body>
</html>

index.ftl

<html>
<head>
    <title>freemarker测试</title>
</head>
<body>
<h1>this is a ${name} page </h1>
</body>
</html>

编写测试controller 

@Controller
public class HelloController {

    @RequestMapping("/hello")
    public String hello(Model model){

        model.addAttribute("name","html");
        return  "hello";

    }
    @RequestMapping("/index")
    public String index(Model model){

        model.addAttribute("name","ftl");
        return  "index";

    }
}

启动观察是否能正常访问,

分别访问是能正常访问的 ,下边把这2个名字都改成hello,看一下显示的是那个页面。

我们可以看到这个是在同一行显示的 ,虽然写的是html页面,但是可以看出这个是ftl的页面,所以说在

 有thymeleaf和freemarker 重名的情况下 ,优先选择了ftl的页面。结束。

原文地址:https://www.cnblogs.com/zwb1234/p/7612969.html