java - 尝试从控制器返回 html 页面时找不到资源
问题描述
我无法在 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={}
解决方案
@Controller
在使用 Spring MVC 时,
您应该始终使用Annotation。@RestController
不同于@Controller
.
@Controller
返回一个视图,用于 MVC 应用程序,
在@RestController
返回响应(主要是 Json)时,它使用 Jackson jar 将您的 java 对象转换为 Json,这些用于 api。
推荐阅读
- c++ - 如何将txt文件中的一行分隔到组件C++
- mule - 如何将字节数组转换为mule中的多部分文件
- r - R - 使用父子关系表计算总数
- java - 没有 minSdkVersion 28 无法在 Android Studio 上构建 apk
- css - 子树枝模板上未继承的 CSS
- f# - Elmish App:动态更改样式
- angular - 如何获取作为对象对象显示到控制台的对象的属性
- django - Django 信号不在终端中打印消息
- jenkins - Jenkins 远程参数化:ExceedRetryLimitException
- php - Woocommerce - 删除交易详情中的发票 ID(Paypal)