【问题标题】:Spring Boot unable to resolve Freemarker viewSpring Boot 无法解析 Freemarker 视图
【发布时间】:2017-07-08 22:10:45
【问题描述】:

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

白标错误页面

这个应用程序没有明确的 /error 映射,所以你看到 这是一个后备。

2017 年 2 月 19 日星期日 17:59:07 GMT 出现意外错误(type=Not 找到,状态=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";
    }
}

application.properties

spring.freemarker.template-loader-path: /
spring.freemarker.suffix: .ftl

服务器

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);
    }
}

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

【问题讨论】:

    标签: java spring spring-mvc spring-boot freemarker


    【解决方案1】:

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

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

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

    【讨论】:

    • 另外,可以将 spring.freemarker.suffix 属性更改为“.ftl”
    【解决方案2】:

    使用 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>
    

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

    【讨论】:

    • 如果你使用 spring-security ,就像我使用登录页面和 Thymeleaf/FreeMarker 一样,请确保将 @EnableWebSecurity 添加到配置中,以便正确解析资源
    • 如上面链接中所述。模板引擎查看 src/main/resources/templates 而不是 src/main/java/resources/templates
    【解决方案3】:

    application.properties 中的 freemarker 模板目录设置错误。应该是:

    spring.freemarker.template-loader-path=classpath:/templates/
    

    【讨论】:

    • 我认为您的意思是application.properties 文件而不是application.xml 文件。
    • 该更改并没有解决它。
    • @crm 您能否确认您的视图是否被调用?
    • @Ashraf.Shk786 控制器方法被正确调用。
    • 是否需要一些其他配置来解析我没有考虑过的 Freemarker 视图?
    【解决方案4】:

    当我将模板名称作为构造函数参数从控制器方法返回给 ModelAndView 对象时,它开始为我工作,而不仅仅是一个字符串。不知道为什么,但我没有时间弄清楚并正在寻找快速修复。如果有人愿意解释,请做。

    使用 Freemarker 的 Spring Boot 2.0.2.RELEASE。这是application.properties的内容:

    spring.freemarker.enabled=true
    spring.freemarker.template-loader-path=classpath:/templates
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-27
      • 1970-01-01
      • 2018-02-25
      • 1970-01-01
      • 2022-11-16
      • 2018-02-10
      • 2020-12-11
      • 1970-01-01
      相关资源
      最近更新 更多