如何在Spring Boot MVC中查看HTML页面?

时间:2019-06-11 11:09:15

标签: spring-boot spring-mvc

当我输入localhost:8080时,将显示文本“索引”。我希望显示“ hello world”,因为这是index.html的正文内容。我有一个控制器类

package com.steinko.reactspringboottutorial.webserver;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;


@RestController
public class HomeController {
    @RequestMapping("/")

}

和应用程序类:

package com.steinko.reactspringboottutorial.webserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class FrontendWebServer {

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

}

我正在使用Gradle构建应用程序

plugins {
    id 'org.springframework.boot' version '2.1.5.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.steinko.reactspringboottutorial.webserver'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
    developmentOnly
    runtimeClasspath {
        extendsFrom developmentOnly
    }
}

repositories {
    mavenCentral()
}

test {
    useJUnitPlatform()
}

ext {
    set('springCloudVersion', "Greenwich.SR1")
}

dependencies {

    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.cloud:spring-cloud-gcp-starter'
    implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
    testImplementation 'org.junit.jupiter:junit-jupiter-params:5.2.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
}

我的index.html文件放置在/src/main/resources/templates/index.html中  看起来像这样

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title> Todo </title>
</head>

<body>
 <p>hello world</p>
</body>

如何修复此程序,以便显示“ hello world”?

1 个答案:

答案 0 :(得分:0)

您的控制者应为:

@Controller
public class HomeController {

    @RequestMapping("/")
    public String index(){

        return "index";
    }

}

修改

在依赖项中添加org.springframework.boot:spring-boot-starter-thymeleaf