一尘不染

为什么html页面没有在百里香中显示?

spring-boot

我正在使用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


阅读 335

收藏
2020-05-30

共1个答案

一尘不染

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

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

在您的DemoApplication中,您只需要@SpringBootApplication,因为此注释等效于使用@ Configuration,@
EnableAutoConfiguration和@ComponentScan

2020-05-30