Spring Boot无法映射我的HTML文件

发布于 2025-02-06 12:03:24 字数 989 浏览 0 评论 0原文

因此,我基本上有这个控制器:

package com.example.controllor;

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

@Controller
public class Controllors {
  @RequestMapping("/")
  public String index(){
    return "index";
  }
  @RequestMapping("/login")
  public String login(){
    return "login";
  }
  @RequestMapping("/register")
  public String register(){
    return "register";
  }
}

我得到了那些HTML页面: 这些3张图像

index html html工作正常,但登录。 。 (它说Whitelabel错误页面此应用程序没有明确的映射 /错误,因此您将其视为后备。SunJun

12 02:37:23 CEST CEST 2022 2022 出乎意料的错误(type =找不到,状态= 404)。 )

有帮助吗?我失去了

胸腺宣言

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

So basically i got this controller :

package com.example.controllor;

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

@Controller
public class Controllors {
  @RequestMapping("/")
  public String index(){
    return "index";
  }
  @RequestMapping("/login")
  public String login(){
    return "login";
  }
  @RequestMapping("/register")
  public String register(){
    return "register";
  }
}

and i got those html pages :
these 3 images

the index html works fine but login.html and register.html don't work. (it says Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Jun 12 02:37:23 CEST 2022
There was an unexpected error (type=Not Found, status=404). )

any help ? i'm lost

My Thymeleaf declaration

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虚拟世界 2025-02-13 12:03:24

问题在于您的软件包结构。
由于您的主要类位于com.example.demo软件包中,因此Spring会将其视为基本软件包,并扫描其下面的所有子包bean。
因此,您有两个选项

  1. 将控制器类移至com.example.demo.controller fackage。
  2. @springapplication注释中,添加scanbasepackages喜欢@springbootapplication(scanbasepackages =“ com.example”)>

The issue is in your package structure.
As your main class is in com.example.demo package, Spring will consider it as base package and scan all subpackages under it for beans.
So, you have two options

  1. Move your controller classes to com.example.demo.controller package.
  2. In @SpringApplication annotation, add scanBasePackages like @SpringBootApplication(scanBasePackages = "com.example")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文