【问题标题】:Why the html page doesn't get showed in thymeleaf?为什么 html 页面没有显示在 thymeleaf 中?
【发布时间】:2017-04-09 07:42:43
【问题描述】:

我正在使用spring bootthymeleaf 视图引擎。

问题是控制器没有显示正确的 html 页面并且总是显示Whitelabel Error Page

控制器:

package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class GifController {

    @RequestMapping("/greeting")
    public String sayhello() {
        return "greets";
    }
}

resources/templates 路径下的 greets.html 文件:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Insert title here</title>
</head>
<body>
Hello!
</body>
</html>

还有:

@EnableAutoConfiguration
@SpringBootApplication
@ComponentScan
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

还有 pom.xml :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>



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



    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

问题出在哪里?当我浏览到localhost:8080/greeting 时,它会显示whitelabel Error page 而不是greets.html

【问题讨论】:

  • 我也遇到了同样的问题,你解决了吗?

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


【解决方案1】:

我只是一个 spring-boot 学习者,并试图通过解决您的问题来学习它。

我做了一些更改,我创建了一个 Config 类并在其中放入了一些配置

@Configuration
public class Config {
    @Bean
    public ServletContextTemplateResolver templateResolver() {
        ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
        resolver.setPrefix("/WEB-INF/templates/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode("HTML5");
        resolver.setOrder(1);
        return resolver;
    }
}



@SpringBootApplication
@ComponentScan
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

我还把 greet.html 页面放在 /src/main/webapp/WEB-INF/templates/greet.html 我喜欢将 html/jsp 页面放入该位置,您可以使用自己的位置,只需在 Config.java 类中替换该位置 @控制器 公共类 GifController {

    @RequestMapping("/greeting")
    public String sayhello() {
        return "greets";
    }
}

尝试这些更改,然后看看您是否能够在浏览器上看到输出。

【讨论】:

  • 你能分享进口或分享完整的文件吗?
【解决方案2】:

您的控制器和 SpringBoot 应用程序看起来不错。尝试清理并构建您的项目。当您运行 DemoApplication 时,您应该在控制台中看到如下所示

Mapped "{[/greeting]}" onto public java.lang.String com.example.controller.GifController.sayhello()

在你的DemoApplication中你只需要@SpringBootApplication,因为这个注解相当于使用@Configuration、@EnableAutoConfiguration和@ComponentScan

【讨论】:

  • 我测试了你的代码,它对我有用。尝试在模板文件夹中添加 index.html 并在 GifController 中添加另一种方法,例如 @RequestMapping("/") public String hello() { return "index"; }。尝试清理、打包您的项目并进行测试。
【解决方案3】:

我遇到了同样的白标错误。我花了几个小时才弄清楚实际上百里香依赖没有得到正确解决。解决方案既简单又愚蠢。

  1. 停止 tomcat。
  2. 右键单击项目。
  3. Gradle -> 刷新 gradle 项目(或 maven 的类似步骤)
  4. 启动 tomcat。
  5. 如果不起作用,请在 build.gradle 中更改下一行的位置(向上或向下移动),然后执行上述步骤。

    编译('org.springframework.boot:spring-boot-starter-thymeleaf')

对于 maven,在 pom.xml 中

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

重要的是,您必须在 tomcat 未运行时更新您的项目。如果我们使用 devtools,我们会盲目地依赖它来重新启动我们的 servlet 容器和其他东西,但有时手动启动停止会很方便。

【讨论】:

    猜你喜欢
    • 2021-07-20
    • 1970-01-01
    • 2018-06-11
    • 2019-04-05
    • 2022-11-16
    • 2014-03-30
    • 1970-01-01
    • 2015-03-20
    • 2014-05-07
    相关资源
    最近更新 更多