简体   繁体   English

在 Spring 引导中未检测到 FreeMarker 模板

[英]FreeMarker template not detected in Spring Boot

I`m trying to build an app with spring boot using freemarker as template engine.我正在尝试使用 freemarker 作为模板引擎构建一个 spring 引导的应用程序。 I am using Gradle.我正在使用 Gradle。

This is the @RestController class这是@RestController class

@RestController
public class CodeController {
    private CodeService codeService;

    public CodeController(@Autowired CodeService codeService) {
        this.codeService = codeService;
    }

    @GetMapping(value = "/code/{id}")
    public String getCode(Model model, @PathVariable int id) {
        List<CodeSnippet> codeSnippets = codeService.getCodeSnippet(id);

        model.addAttribute("codeSnippets", codeSnippets);
        System.out.println(codeSnippets);
        return "getcode";

    }

}

And this is the FreeMarker template file getcode.ftlh这是 FreeMarker 模板文件 getcode.ftlh

<!DOCTYPE html>
<html>
<head>
    <title>Code</title>
    <meta charset="UTF-8" >
    <style>
        #load_date {
            color: green;
        }

        pre {
            margin-top: 0;
            border: solid black 1px;
            background-color: lightgray;
            padding: .3em
        }
    </style>
</head>
<body>
<#list codeSniptets as codeSnippte>
    <span id=\"load_date\">${codeSnippte.date}</span>
    <pre id=\"code_snippet\">${codeSnippte.code}</pre>
</#list>

</body>
</html>

Spring boot should load the codeSnipptets list to the template. Spring 引导应将 codeSnipptets 列表加载到模板。 However, I just receive "getcode" string as the output instead of the HTML page when the page loaded with a browser or postman.但是,当页面使用浏览器或 postman 加载时,我只收到“getcode”字符串作为 output 而不是 HTML 页面。

Not sure what I am doing wrong.不知道我做错了什么。

I hope you already configures FreeMarkerViewResolver我希望你已经配置了 FreeMarkerViewResolver

and instead of RestController use Controller而不是 RestController 使用 Controller

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM