【问题标题】:Spring boot returning error 404 html page not foundSpring Boot 返回错误 404 html 页面未找到
【发布时间】:2020-11-13 10:25:25
【问题描述】:

我正在尝试返回位于 src/main/resources/templates 下的 index.html,但它似乎没有加载。

堆栈跟踪

2020-07-24 00:09:11.881 DEBUG 17204 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : GET "/", parameters={}
2020-07-24 00:09:11.884 DEBUG 17204 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.Alex.Flights.FlightsController#getIndexForm(ModelMap)
2020-07-24 00:09:11.885 DEBUG 17204 --- [nio-8080-exec-1] o.j.s.OpenEntityManagerInViewInterceptor : Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
2020-07-24 00:09:11.902 DEBUG 17204 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, application/xhtml+xml, image/webp, application/xml;q=0.9, */*;q=0.8]
2020-07-24 00:09:11.902 DEBUG 17204 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView  : View name 'index', model {userForm=Flights [flightNumber=dummy, origin=dummy, destination=dummy, takeOffTime=dummy, landingTime=dummy, flightDuration=dummy, takeOffDate=Fri Jul 24 00:09:11 SGT 2020, landingDate=Fri Jul 24 00:09:11 SGT 2020, flightReturn=false], org.springframework.validation.BindingResult.userForm=org.springframework.validation.BeanPropertyBindingResult: 0 errors}
2020-07-24 00:09:11.903 DEBUG 17204 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView  : Forwarding to [index]
2020-07-24 00:09:11.905 DEBUG 17204 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : "FORWARD" dispatch for GET "/index", parameters={}
2020-07-24 00:09:11.907 DEBUG 17204 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"]
2020-07-24 00:09:11.908 DEBUG 17204 --- [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found

控制器中的方法

 package com.Alex.Flights;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String getIndexForm(ModelMap model) {
    model.addAttribute("userForm", new Flights("dummy", "dummy", "dummy", "dummy", "dummy","dummy", new Date(), new Date(), false));
    return "index";
}

主要

package com.Alex.Mains;

@SpringBootApplication
@ComponentScan(basePackages = {
        "com.Alex.JPA", "com.Alex.UserPackage", "com.Alex.Flights"
})
@EntityScan( basePackages = {"com.Alex.UserPackage", "com.Alex.Flights"})
@EnableJpaRepositories({"com.Alex.UserPackage", "com.Alex.Flights"})
public class JpaApplication {

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

}

【问题讨论】:

    标签: spring spring-boot spring-mvc web-applications controller


    【解决方案1】:

    它似乎只在 /resources 之类的文件夹中查找,而不是在 /templates 中查找。

    解决方案是定义你自己的 ViewResolver bean 并设置它 https://www.baeldung.com/spring-mvc-view-resolver-tutorial

    【讨论】:

    • 我尝试将 .html 放入资源中,但没有成功。我认为放置不是问题,我的其他项目在模板中使用它,也没有自定义视图解析器
    • 如果你在 springboot 中添加 thymeleaf,它会生成它自己的 viewresolver 并在最后添加 .html。如果没有 thymeleaf 或其他视图解析器,它将不知道在哪里查找或添加 .html
    猜你喜欢
    • 2019-09-16
    • 1970-01-01
    • 2018-08-08
    • 2022-01-05
    • 2021-10-13
    • 2020-04-12
    • 1970-01-01
    • 2012-09-15
    • 2020-03-08
    相关资源
    最近更新 更多