【问题标题】:Spring Boot @Getmapping returns a 404 whitelabel error page in a very simple projectSpring Boot @Getmapping 在一个非常简单的项目中返回 404 whitelabel 错误页面
【发布时间】:2021-10-17 05:20:33
【问题描述】:

我正在学习弹簧靴。我创建了这个超级简单的项目,但是当我尝试返回带有 @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";
    }
}

这里是spring应用:

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);
    }


}

Here is the directory

这里是 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 错误页面。

【问题讨论】:

    标签: java spring-boot get-mapping


    【解决方案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;
        }
    

    【讨论】:

    • 哇,非常感谢!看来我不明白静态文件夹和模板文件夹之间的区别。我正在关注另一个使用 thymleaf 并将 HTML 文件放在模板中的教程,这就是我在这里感到困惑的原因。再次感谢!
    猜你喜欢
    • 2017-10-06
    • 2019-04-02
    • 2018-07-22
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    • 2018-07-27
    • 2018-06-09
    • 1970-01-01
    相关资源
    最近更新 更多