【问题标题】:Springboot cannot find templatesSpring Boot 找不到模板
【发布时间】:2021-09-04 18:36:07
【问题描述】:

它是我的控制器

@Controller
@RequestMapping("/user")
public class UserController {

    @GetMapping("/login")
    public String login(){
        System.out.println("hello");
        return "user/login";
    }
}

这是我的 yaml 文件

spring:
  thymeleaf:
    check-template-location: true
    prefix: classpath:/templates/
    suffix: .html
    cache: false
    mode: HTML
    encoding: utf-8

这是我的目录树

├─src
│  ├─main
│  │  ├─java
│  │  │  └─com
│  │  │      └─example
│  │  │          └─facebook
│  │  │              ├─controller
│  │  │              ├─dao
│  │  │              ├─model
│  │  │              └─service
│  │  └─resources
│  │      ├─mapper
│  │      ├─static
│  │      └─templates
│  │          ├─hello
│  │          └─user
|                └─ login.html
│  └─test
│      └─java
│          └─com
│              └─example
│                  └─facebook

但是...我的项目找不到带有 404 错误的 login.html,但在控制台中打印 hello... 我该如何解决?

【问题讨论】:

  • 你在 YAML 中所做的配置正常应该不需要。您尝试在浏览器中访问的确切 URL 是什么?有打印异常吗?

标签: spring-boot intellij-idea thymeleaf


【解决方案1】:

由于您提到控制器打印“hello”字符串,因此映射正在工作。您是否在 pom 文件中添加了 thymeleaf 依赖项?

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

【讨论】:

    猜你喜欢
    • 2017-06-21
    • 1970-01-01
    • 2020-11-21
    • 2019-02-07
    • 2017-05-10
    • 2015-01-03
    • 1970-01-01
    • 2018-11-01
    • 2016-01-26
    相关资源
    最近更新 更多