【发布时间】: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 intarget 上找到它,是不是错了?附:目前项目中没有配置maven资源插件。 -
您能否展示一下您的 MVC 配置类?您配置了
template resolverbean 的那个。通常它是一个实现WebMvcConfigurer的@Configuration类。如果您没有创建它 - 您的 Spring 可能不知道您的控制器方法的返回是模板的路径
标签: java spring-boot intellij-idea thymeleaf