java - 尝试从 Controller 返回 html 页面时找不到资源

标签 java spring-boot

我无法在 springboot 中呈现 html 页面。 这是代码...

@RestController
    public class ProductController {
        @Autowired
        ProductService service;
    
        @InitBinder
        public void initBinder(WebDataBinder webDataBinder) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
            dateFormat.setLenient(false);
            webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        }
        
        @RequestMapping(value = { "/", "/home" })
        public ModelAndView home() {
            System.out.println("sdasasas");
            return new ModelAndView("home");
        }
    
    

但每当我点击 http://localhost:8080/home 时,它​​都会显示以下日志

-8080-exec-4] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, application/xhtml+xml, image/webp, image/apng, application/signed-exchange;v=b3, application/xml;q=0.9,*/*;q=0.8]
2019-07-31 16:05:25.354 DEBUG 14850 --- [nio-8080-exec-4] o.s.w.servlet.view.InternalResourceView  : View name 'home', model {}
2019-07-31 16:05:25.354 DEBUG 14850 --- [nio-8080-exec-4] o.s.w.servlet.view.InternalResourceView  : Forwarding to [/WEB-INF/html/home.html]
2019-07-31 16:05:25.354 DEBUG 14850 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : "FORWARD" dispatch for GET "/WEB-INF/html/home.html", parameters={}
2019-07-31 16:05:25.356 DEBUG 14850 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/","/"]
2019-07-31 16:05:25.356  WARN 14850 --- [nio-8080-exec-4] o.s.w.s.r.ResourceHttpRequestHandler     : Path with "WEB-INF" or "META-INF": [WEB-INF/html/home.html]
2019-07-31 16:05:25.356 DEBUG 14850 --- [nio-8080-exec-4] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found
2019-07-31 16:05:25.357 DEBUG 14850 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Exiting from "FORWARD" dispatch, status 404
2019-07-31 16:05:25.357 DEBUG 14850 --- [nio-8080-exec-4] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2019-07-31 16:05:25.357 DEBUG 14850 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Completed 404 NOT_FOUND
2019-07-31 16:05:25.357 DEBUG 14850 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for GET "/error", parameters={}

最佳答案

对于必须返回 View 的基于 MVC 的应用程序,避免@RestController。它主要用于 REST API。 @Controller 可以返回一个 View

关于@RestController的更多信息:

enter image description here

  • 这个注释是 @Controller 的一个特殊版本,它添加了 @Controller@ResponseBody 自动注解。所以我们不必将 @ResponseBody 添加到我们的映射方法中。这意味着 @ResponseBody 默认激活。
  • 如果你使用 @RestController 你不能返回一个 View (通过使用 Viewresolver 在 Spring/Spring-Boot 中)
  • @RestController 还将响应转换为 JSON/XML 自动,因为 @ResponseBody 使返回的对象成为可能在正文中的对象, 例如JSON 或 XML

Controller vs RestController


@RestController
    public class ProductController {
        @Autowired
        ProductService service;

        @RequestMapping(value = { "/", "/home" })
        public @ResponseBody ModelAndView home() {
            System.out.println("sdasasas");
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("home");
            return modelAndView;
        }
}

项目结构 enter image description here

无需指定spring.mvc.view.prefix=/WEB-INF/html/spring.mvc.view.suffix=.html

此外,请确保您没有任何带有 @EnableWebMvc 注释的附加类。这可能会弄乱 spring-boot 自动配置。

关于java - 尝试从 Controller 返回 html 页面时找不到资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57288932/

相关文章:

java - 与 ArrayList 的合并排序

java - 解决 MongoDB 错误

java - XML/REST Spring Web 服务中未考虑 @XmlElementWrapper

java - 如何在 Angular、Spring-Boot、Maven 项目中配置项目自动重新加载浏览器

java - 另一台服务器上的 Spring Boot 外部配置

java - Android 中的 HashSet 空指针异常但在 java 中没有发生。为什么?

java - Spring security_密码加密

Java - PDF 到 PCL

spring-boot - Spring Boot 2.3.3.Rlease与Gradle包装器v6.6.1和CircleCI-如何使CircleCI不使用Gradle v6.2.2?

java - map 的配置属性