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