0

我正在学习弹簧靴。我创建了这个超级简单的项目,但是当我尝试返回带有 @GetMapping 注释的 HTML 页面时,我不断收到 404 Whitelabel 错误页面。

这是我唯一的控制器:

package com.example.springplay;

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

@Controller
public class MainController {
    @GetMapping(value = "/")
    public String hello(){
        return "hello";
    }
}

这是弹簧应用程序:

package com.example.springplay;

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


@SpringBootApplication
public class SpringplayApplication {

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


}

这是目录

这是 hello.html 页面:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>hello world</h1>
</body>
</html>

hello.html页面在resources/templates文件夹下,不知道怎么回事。我从另一个工作项目中复制了具有完全相同结构的这部分,但我的只是给了我这个 Whitelabel 错误页面。

4

1 回答 1

1

将 hello.html 移动到资源中的静态文件夹并更改控制器,如下所示:

    @GetMapping(value = "/")
    public String hello(){
        return "hello.html";
    }

或像这样:

    @GetMapping(value = "/")
    public ModelAndView  hello(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("hello.html");
        return modelAndView;
    }
于 2021-10-17T07:05:18.623 回答