为什么 html 页面没有显示在 thymeleaf 中? [英] Why the html page doesn't get showed in thymeleaf?

查看:33
本文介绍了为什么 html 页面没有显示在 thymeleaf 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using spring boot and the thymeleaf view engine.

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

The problem is the controller doesn't show the proper html page and always shows the 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";
    }
}

资源/模板路径中的greets.html文件:

The greets.html file in resources/templates path:

<!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 :

And the 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.

Where's the problem? When I browse to localhost:8080/greeting it shows whitelabel Error page instead of the greets.html.

推荐答案

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

Your Controller and SpringBoot application looks ok. Try to clean and build your project. When you will run DemoApplication you should see like below in console

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

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

And in your DemoApplication you only need @SpringBootApplication as this annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan

这篇关于为什么 html 页面没有显示在 thymeleaf 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆