【发布时间】:2017-12-15 19:42:24
【问题描述】:
我使用 SpringBoot 1.5.9 并创建了src/main/resources/static/index.html:
<!DOCTYPE html><html><head><meta charset="utf-8"/>
<title>Home</title></head>
<body><h2>Hello World!!</h2></body>
</html>
我的简单控制器正在处理“/”并转发到 index.html:
@Controller
public class MyController {
@RequestMapping("/")
public String home(Model model) {
System.out.println("In MyController!!!");
return "index";
}}
但是,当我从以下位置运行我的主要方法后:
@SpringBootApplication
public class MySpringBoot2Application {
public static void main(String[] args) {
SpringApplication.run(MySpringBoot2Application.class, args);
}
}
然后我将浏览器指向:http://localhost:8080/
我正进入(状态:
白标错误页面-出现意外错误(type=Not Found, status=404)
但是,如果我尝试直接访问该页面 - 它工作正常: http://localhost:8080/index.html
根据 SpringBoot 文档,src/main/resources/static/ 下的静态内容应该被渲染。
如果我创建文件夹src/main/resources/templates/ 并将我的index.html 放在那里,并在我的 pom.xml 中包含 spring-boot-starter-thymeleaf 依赖项,那么一切正常。但是我很困惑为什么 src/main/resources/static/index.html 的这个基本渲染
不管用。任何帮助表示赞赏。
【问题讨论】:
-
删除你的
MyController类,没有它它应该可以工作
标签: spring-boot