我正在按照本教程将 Spring Boot 与 AngularJS 集成:
https://dzone.com/articles/java-8-springboot-angularjs-bootstrap-springdata-j
我在 Eclipse/Photon 中使用 STS 构建了 Spring Boot 启动器。我的 Eclipse 项目如下所示:
我的“Home”控制器如下所示:
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping("/home")
public String home() {
return "index";
}
}
根据上面的屏幕截图,我在 src/main/resources/static/views 中有一个“index.html”。
我的application.properties定义了spring.mvc.view.prefix和.suffix:
spring.mvc.view.prefix = /views/
spring.mvc.view.suffix = .html
右键单击 Eclipse 项目并选择“Run As > Spring App”时,没有任何错误或警告。
但是当我浏览到 localhost:8080/home 时,我得到:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Nov 03 13:40:19 PDT 2018
There was an unexpected error (type=Not Found, status=404).
No message available
为什么我在尝试访问“/home”时收到“404”?
我需要在 Eclipse 中对我的 Eclipse 项目做一些“特殊”的事情吗?
有哪些好的故障排除策略?
我相信您没有使用诸如
thymeleaf
之类的模板引擎。
尝试以下操作,应该可以解决您的问题。
index.html
移至 src/main/resources/static
。这是自动提供静态资源(如 HTML
文件)的位置。 application.properties
文件中定义的这两行。