Spring Boot 无法解析 Freemarker 视图

crm*_*ham 5 java spring freemarker spring-mvc spring-boot

我第一次尝试使用 Spring Boot 显示 Freemarker 视图,目前在浏览器中出现以下错误:

白标错误页面

此应用程序没有明确的 /error 映射,因此您将其视为后备。

Sun Feb 19 17:59:07 GMT 2017 出现意外错误(类型=未找到,状态=404)。没有可用的消息

我正在使用 Spring Boot 1.5。

我的文件结构:

在此处输入图片说明

登录控制器

package com.crm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.crm.service.LoginService;

@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;

    @RequestMapping("/login")
    public String authenticate() {
        return "loginPage";
    }
}
Run Code Online (Sandbox Code Playgroud)

应用程序属性

spring.freemarker.template-loader-path: /
spring.freemarker.suffix: .ftl
Run Code Online (Sandbox Code Playgroud)

服务器

package com.crm;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Server{

    public static void main(String[] args) {
        SpringApplication.run(Server.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么 Spring 无法解析 loginPage.ftl 视图?为什么我在网络浏览器中看不到它?

Jan*_*nar 16

Spring Boot 最近再次引入了一个重大更改,这导致了臭名昭著的whitelabel error页面。他们将默认后缀从 更改.ftl.ftlh

https://github.com/spring-projects/spring-boot/issues/15131

因此对于 Spring Boot 2.2.x 应用程序,这些扩展必须更新。


rdl*_*pes 5

使用 Spring Boot 1.5.1,您不需要将这些属性添加到 application.properties 文件中,由于自动配置,它们已经定义了。

删除这 2 个属性,它应该与 pom.xml 中的以下依赖项一起使用:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

文档可以在这里找到:http://docs.spring.io/spring-boot/docs/1.5.1.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-template-engines