【问题标题】:What is reason controller not able to return the view page控制器无法返回视图页面的原因是什么
【发布时间】:2022-01-16 21:40:46
【问题描述】:

我使用 thymeleaf 作为前端,使用 springboot 作为后端。但 当我在注册控制器中返回我的 register.html 页面时,然后运行 应用程序 。在我的 chrome 浏览器中点击 localhost:8080 时 显示错误 404,找不到页面。我将我的设计页面定义在 模板文件夹 这是项目结构

StudentController.kt

 @GetMapping("/")
    fun home(student: Student, model: Model):String{
        model.addAttribute("courses",courses)
        return "register"
    }

    /**Registration API for Student Registration*/
    @PostMapping("/register")
    fun register(@ModelAttribute("student")student: Student, model: Model):String{
        println("Details of the course Details ${student.course}")
        studentService.saveStudent(student)
        return "welcome"
    }

当我点击 localhost:8080 时,它会显示以下错误类型

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Dec 13 17:09:34 IST 2021
There was an unexpected error (type=Not Found, status=404).

【问题讨论】:

  • 我认为您没有任何根路径“/”的映射。 404 错误通常意味着服务器找不到给定路径的响应
  • 另外,当 Spring boot 遇到错误时,它会寻找映射到 /error 的视图。在您的情况下,也添加一个 error.html 模板

标签: spring-boot thymeleaf


【解决方案1】:

您是否在 application.properties 中设置了 thymeleaf 前缀? spring.thymeleaf.prefix=file:./templates/

【讨论】:

    【解决方案2】:

    application.properties 中添加prefixsuffix

    spring.thymeleaf.prefix= classpath:/templates/
    spring.thymeleaf.suffix= .html
    

    【讨论】:

      【解决方案3】:

      Thymeleaf 前缀的默认值是 classpath:/templates/ 和后缀 .html source. 这似乎是正确的,但您可以检查您是否在 yml/properties 配置中覆盖了这些值。还要检查您是否在 Gradle 中添加了 Thymeleaf 作为依赖项,如有必要,请重新加载 Gradle 项目。最后,检查你的控制器类上是否有@Controller 注解以及适当的@RequestMapping。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-25
        • 2013-05-25
        • 1970-01-01
        相关资源
        最近更新 更多