【问题标题】:Sprint Boot Web Application with Maven not returning HTML page带有 Maven 的 Spring Boot Web 应用程序不返回 HTML 页面
【发布时间】:2019-01-23 00:31:13
【问题描述】:

我使用 Maven 创建了一个简单的 Spring Boot Web 应用程序。尝试在点击 / 页面时返回一个 HTML 页面,但它没有返回抛出白标错误页面。

我的主要课程:

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

@SpringBootApplication
public class ProductLaunchReportingApplication {

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

控制器:

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

@Controller
//@ResponseBody
public class HomeController {

    @RequestMapping("/")
    public String viewHome() {
        return "home";
    }

}

应用程序属性:

server.port=9991

home.html :(位置:src/main/resources/static)

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Home Page</title>
</head>
<body>
<p>Hello From Home Page</p>
</body>
</html>

【问题讨论】:

  • 你能不能显示你的 pom.xml 以及 ProductLaunchReportingApplication 和 HomeController 在哪个包中
  • stackoverflow.com/a/51555158/6446770 看到这个以获得详细的答案。也阅读 cmets 以获得更好的理解。
  • src/main/resources/static 下的东西不是使用 ViewResolver 解决的,而是直接解决的。所以/home.html 会起作用。如果这不是您想要的,请包含 Thymeleaf 之类的内容并将文件放在 src/main/resources/templates 而不是 static

标签: java spring maven spring-boot


【解决方案1】:

将您的 HTML 放在 src/main/resources/templates/home.html 中即可。


您可以关注this link以获得更多帮助。

【讨论】:

  • @RestController 将只返回字符串“home”而不是 html 页面。
  • 更新了我的答案。
【解决方案2】:

当您使用 Spring Boot 时。你唯一需要让它工作的是这个依赖:

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

并且你需要在 resources/templates/home.html 中添加你的 html 文件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 2016-02-11
    • 2018-11-03
    相关资源
    最近更新 更多