簡體   English   中英

在 Spring 引導中未檢測到 FreeMarker 模板

[英]FreeMarker template not detected in Spring Boot

我正在嘗試使用 freemarker 作為模板引擎構建一個 spring 引導的應用程序。 我正在使用 Gradle。

這是@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";

    }

}

這是 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 引導應將 codeSnipptets 列表加載到模板。 但是,當頁面使用瀏覽器或 postman 加載時,我只收到“getcode”字符串作為 output 而不是 HTML 頁面。

不知道我做錯了什么。

我希望你已經配置了 FreeMarkerViewResolver

而不是 RestController 使用 Controller

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM