【问题标题】:Cannot open index page in Thymeleaf, getting 404 in Intellij IDEA无法在 Thymeleaf 中打开索引页面,在 Intellij IDEA 中得到 404
【发布时间】:2022-01-15 17:46:57
【问题描述】:

我为 Spring boot 和 Thymeleaf 创建了简单的演示,但我无法打开索引页面。

控制器代码:

package com.jrp.demo.web;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
@Controller
public class HomeController {
 
    @RequestMapping(value = "/")
    public String home() {
        return "index";
    }
}

在/src/main/resources/templates/index.html中有索引模板

拥有application.properties:

server.port=9095

然后转到地址http://localhost:9095/

得到错误:

2021-12-10 11:08:32.200 DEBUG 11124 --- [nio-9095-exec-2] o.s.web.servlet.DispatcherServlet        : GET "/", parameters={}
2021-12-10 11:08:32.200 DEBUG 11124 --- [nio-9095-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.jrp.demo.web.HomeController#home()
2021-12-10 11:08:32.201 DEBUG 11124 --- [nio-9095-exec-2] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.9, */*;q=0.8]
2021-12-10 11:08:32.201 DEBUG 11124 --- [nio-9095-exec-2] o.s.w.servlet.view.InternalResourceView  : View name 'index', model {}
2021-12-10 11:08:32.201 DEBUG 11124 --- [nio-9095-exec-2] o.s.w.servlet.view.InternalResourceView  : Forwarding to [index]
2021-12-10 11:08:32.201 DEBUG 11124 --- [nio-9095-exec-2] o.s.web.servlet.DispatcherServlet        : "FORWARD" dispatch for GET "/index", parameters={}
2021-12-10 11:08:32.202 DEBUG 11124 --- [nio-9095-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
2021-12-10 11:08:32.204 DEBUG 11124 --- [nio-9095-exec-2] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found
2021-12-10 11:08:32.204 DEBUG 11124 --- [nio-9095-exec-2] o.s.web.servlet.DispatcherServlet        : Exiting from "FORWARD" dispatch, status 404
2021-12-10 11:08:32.204 DEBUG 11124 --- [nio-9095-exec-2] o.s.web.servlet.DispatcherServlet        : Completed 404 NOT_FOUND
2021-12-10 11:08:32.204 DEBUG 11124 --- [nio-9095-exec-2] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for GET "/error", parameters={}
2021-12-10 11:08:32.207 DEBUG 11124 --- [nio-9095-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
2021-12-10 11:08:32.220 DEBUG 11124 --- [nio-9095-exec-2] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
2021-12-10 11:08:32.220 DEBUG 11124 --- [nio-9095-exec-2] o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 404

Spring boot 类是:

package com.jrp.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

并且 pom.xml 包含 spring-boot-starter-thymeleaf 和 spring-boot-starter-web。

【问题讨论】:

  • 你能给我看看你的index.html和application.properties
  • 您的资源也打包正确了吗?检查target 文件夹的内容。如果 html 模板在您的控制器中的路径下不存在 - 尝试清理重建您的应用程序并考虑检查您的 Maven Resources 插件(如果您使用它)
  • @FaeemazazBhanej application.properties 有问题描述(只有server.port 存在)并且index.html 仅包含<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>This is index page</h1> </body> </html>
  • @PavelPolyakoff 我在项目上运行了clean install 并在目标中寻找index.html 并在路径..\demo1\target\demo-0.0.1-SNAPSHOT.jar\BOOT-INF\classes\templates` but have not found it directly in target 上找到它,是不是错了?附:目前项目中没有配置maven资源插件。
  • 您能否展示一下您的 MVC 配置类?您配置了template resolver bean 的那个。通常它是一个实现WebMvcConfigurer 的@Configuration 类。如果您没有创建它 - 您的 Spring 可能不知道您的控制器方法的返回是模板的路径

标签: java spring-boot intellij-idea thymeleaf


【解决方案1】:

如following answer 中所述,Intellij IDEA 具有过时的依赖项,因此帮助按钮 Reload all maven projects 和 Generate sources and update folders for all projects。

【讨论】:

    猜你喜欢
    • 2015-04-26
    • 1970-01-01
    • 2020-12-02
    • 1970-01-01
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多