Springboot 白标错误页面 [英] Springboot Whitelabel Error Page

查看:87
本文介绍了Springboot 白标错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坚持使用这个简单的 MVC 示例.当我启动应用程序并转到 localhost:8080 时,我得到了白标错误页面",即使我在src/main/resources/templates"中创建了index.html".我还在我的 index 方法上添加了 @RequestMapping("/") .我找不到问题.

I stuck with this simple MVC example. When I start the App and go to localhost:8080, I got "Whitelabel Error Page", even I created "index.html" in "src/main/resources/templates". I also add @RequestMapping("/") on my index method. I can't find the problems.

IndexController.java:

package controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
  @RequestMapping("/")
  public String index(){
    return "index";
  }
}

SpringmvcApplication.java:

package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringmvcApplication {
  public static void main(String[] args) {
    SpringApplication.run(SpringmvcApplication.class, args);
  }
}

index.html - 在src/main/resources/templates"下:

index.html - under "src/main/resources/templates":

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<title>Hello Spring MVC</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h1>Hello World</h1>
<h2>This is my Thymeleaf index page.</h2>
</body>
</html>

推荐答案

如您在日志中所见,Spring 未找到并注册您的控制器.可能是因为它属于没有自动扫描类的包.为了解决这个问题,我建议将代码结构更新为 文档中建议的结构.另一种修复方法是尝试手动指定 @ComponentScan.

As you can see in the log your controller wasn't found and registered by Spring. Probably, because it belongs to the package that wasn't auto scanned for the classes. To fix so, I suggest to update the structure of the code to the structure that is advised in the documentation. Another way to fix it is to try to specify @ComponentScan manually.

这篇关于Springboot 白标错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆