【问题标题】:Spring Boot v2.0 doesn't see templateSpring Boot v2.0 看不到模板
【发布时间】:2018-06-26 09:26:54
【问题描述】:

我尝试启动在Spring Initializr 中创建的演示应用程序。 Spring Boot 版本 - 2.0.3。

我创建了 index.html 并将其放入 resources/templates 文件夹中。当我启动 spring-boot:run 时,控制台中的一条消息表明:

Tomcat started on port(s): 8080 (http) with context path ''

当我打开 localhost:8080 时,它给了我 Spring 的 Whitelabel 错误页面

我尝试在 resources/application.properties 文件中添加行 server.servlet.context-path=/。但这无济于事。

如何为索引设置正确的路径?

UPD:似乎问题仅出在模板部分。

resources/templatesindex.html的内容:

<!DOCTYPE html>
<html lang="en">

<body>
<h1>Hello {{ name }}!!!</h1>
</body>
</html>

IndexController.java的内容:

@Controller
public class IndexController {
    @GetMapping("/")
    public ModelAndView index() {
        Map<String, String> model = new HashMap<>();
        model.put("name", "world");
        return new ModelAndView("index", model);
    }

我用小胡子,pom.xml:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mustache</artifactId>
    </dependency>
    <dependency>

它给了我白标错误。即使在resources/application.properties 中有server.servlet.context-path=/

如果我将文件放在resource/static 中,并在IndexController 中将文件重命名为“index.html”,则会显示,但“名称”显然没有被替换。

我做错了什么?

【问题讨论】:

    标签: java spring spring-boot tomcat


    【解决方案1】:

    最后,this answer 提供了帮助。似乎它的小胡子功能。这很奇怪,因为在 SpringBoot 1.5.x 中,空文件 application.properties 一切正常。如果有人知道,为什么它会更改为 SpringBoot 2.0 - 在 cmets 中分享。

    【讨论】:

      【解决方案2】:

      如果您想提供静态内容,您需要将其放入您的resources 文件夹中的static 文件夹中。

      templates 在您使用 Thymeleaf 和 Spring MVC 控制器等视图技术时使用。

      您可以在这里找到更多信息:https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot

      【讨论】:

      • 我使用 mustache 模板引擎。无论如何,将我的 index.html 移动到 staticfolder 后,我得到了相同的结果
      【解决方案3】:

      Spring Boot 将请求定向到匹配的控制器。因此,定义一个简单的 Controller 类来处理请求并将该类放在项目根包下方或内部的包中:

      @Controller
      public class MyController {
      
         @GetMapping(value={"/"})
         public ModelAndView getRoot()
         {
             return new ModelAndView("index");
         }
      }
      

      Controller-Annotation 来自 org.springframework.stereotype 包,GetMapping-Annotation 来自 org.springframework.web.bind.annotation。

      也许您还需要扩展您的 pom.xml 文件。检查它是否包含依赖 spring-boot-starter-web (如果您选择了“Maven Project”)。

      【讨论】:

      • 我有一个控制器。 IDE 导入您提到的包,并且 pom 文件包含此依赖项。尽管如此,没有任何效果,我猜,这是因为上下文路径未设置为“\”。但我不知道如何正确设置它,因为application.property 文件没有帮助
      • 输出“带有上下文路径''”不应该打扰你。这就是默认情况下所说的。随意通过在 application.properties 文件中设置 server.servlet.context-path 来更改上下文根(对于旧版本的 spring boot,使用 server.context-path)。但是我在这里没有看到问题(只是检查了我的一些微服务:它们都说“with context path ''”并且工作正常)。
      • 您还可以将映射更改为值列表。随意尝试@GetMapping(value{"/", ""})
      • 啊哈,我找到了一些东西。仅当我将“index.html”而不是“index”提供给 ModelAndView 构造函数时,它才会显示页面。并且它不会用给构造函数的映射中的 var 值替换 {{ var }}。我想,模板有问题...我使用教程中的spring-boot-starter-mustache...我不知道
      • 更新问题。 @Flocke,你能检查一下吗?
      【解决方案4】:

      你应该改变 application.properties

      spring.mustache.suffix:.html
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-04
        • 2015-09-14
        • 2017-06-21
        • 2016-01-26
        • 2015-07-04
        • 2020-03-14
        • 2017-03-08
        • 2020-11-21
        相关资源
        最近更新 更多