Spring boot:配置它找到webapp文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28725635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring boot: configure it to find the webapp folder
提问by user2191332
By default, Spring Boot looks in my src/main/webapp folder to find my html files. Where can I change the settings for Spring Boot if I use another folder to put the html files?
默认情况下,Spring Boot 在我的 src/main/webapp 文件夹中查找我的 html 文件。如果我使用另一个文件夹来放置 html 文件,我在哪里可以更改 Spring Boot 的设置?
Later, the files will be wrapped in jars for deployment. Are there other things I need to worry about?
稍后,这些文件将被包装在 jar 中以进行部署。还有其他我需要担心的事情吗?
回答by Evgeni Dimitrov
请参阅文档:http: //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-static-content
The static resources are loaded from /static
, /public
, /resources
, /META-INF/resources
静态资源从/static
、/public
、/resources
、/META-INF/resources
If you are packaging your application as a JAR (deploying a .jar), using src/main/webapp
is not recommended.
如果您将应用程序打包为 JAR(部署 .jar),src/main/webapp
则不建议使用。
You can customize that by overriding the addResourceHandlers
method in WebMvcConfigurerAdapter
您可以通过覆盖中的addResourceHandlers
方法来自定义WebMvcConfigurerAdapter
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){
registry.addResourceHandler("/**")
.addResourceLocations("/")
.setCachePeriod(0);
}
}
回答by akash
AS mentioned above, jar
packaging ignores webapp
content. If you still want to include the contents inside webapp
folder, you need to specify this explicitly in maven POM.xml
configuration as below -
如上所述,jar
包装忽略webapp
内容。如果您仍想在webapp
文件夹中包含内容,则需要在 MavenPOM.xml
配置中明确指定,如下所示 -
<build>
<resources>
<resource>
<directory>src/main/webapp</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources> ...