Spring Boot Whitelabel 错误页面

2023-12-10

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

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”下:

<!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手动。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Boot Whitelabel 错误页面 的相关文章

随机推荐