无法在 Thymeleaf 中打开索引页,在 Intellij IDEA 中获取 404
Cannot open index page in Thymeleaf, getting 404 in Intellij IDEA
我已经为 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开机class是:
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.
如, Intellij IDEA had outdated dependencies, so helped buttons Reload all maven projects
and Generate sources and update folders for all projects
.
所述
我已经为 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开机class是:
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.
如Reload all maven projects
and Generate sources and update folders for all projects
.