我从 spring.io 阅读了以下文档它说 默认情况下 Spring Boot 将提供来自类路径中名为/static (或/public 或/resources 或/META-INF/resources)的目录的静态内容
但是当我把我的 index.html /resources
下的文件中,字符串 index
刚刚被渲染。目前 index.html
在 webapp 下,我正在使用 AngularJS。
Mvc配置
@Configuration
public class MvcConfig {
@Bean
InternalResourceViewResolver viewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/webapp/");
resolver.setSuffix(".html");
return resolver;
}
}
索引页面的 Restful 服务
@RestController
public class IndexController {
@RequestMapping("/")
public String index(){
System.out.println("Looking in the index controller.........");
return "index";
}
}
在我的 IDE 控制台上,我确实看到 Looking in the index controller......
从 IndexController 打印,并且在 Chrome 开发工具的网络下我只看到 localhost 200
.
index.html
<body>
<!--[if lt IE 7]>
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<div ng-view></div>
<div>Angular seed app: v<span app-version></span></div>
最佳答案
Do not use the src/main/webapp directory if your application will be packaged as a jar. Although this directory is a common standard, it will only work with war packaging and it will be silently ignored by most build tools if you generate a jar.
Spring Boot 非常固执己见,并且在您不尝试抵制默认设置时效果最佳。我看不出有任何理由将您的文件放在 /src/main/webapp
中。只需将 /src/main/resources/static
用于您的前端 Assets 。这是最常见的地方。
它将自动从根 URI 提供这些静态文件,而无需创建任何根级 Controller
。实际上,您的 IndexController
会阻止从根 URI 提供静态前端文件。完全不需要为静态文件创建Controller
。
您的应用也不需要 View 解析器。您的应用程序只是单页 Angular 应用程序使用的 REST API。所以你的 HTML 模板在客户端。如果您正在做服务器端 HTML 模板(例如使用 Thymeleaf 或 JSP),则需要 View 解析器。所以也删除那 block 。
关于java - Spring boot在webapp文件夹下找不到index.html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35968479/