webapp文件夹 放入 src/main 下:
方法1:
在pom 文件添加:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/webapp</directory>
<!--注意此次必须要放在此目录下才能被访问到 -->
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/**</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
</build>
方法2:
增加一个配置类:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.File;
@Configuration
public class TomcatConfig {
@Value("${tomcat.factory.doc.root}")
private String rootDoc;
@Bean
public AbstractServletWebServerFactory embeddedServletContainerFactory() {
TomcatServletWebServerFactory tomcatServletWebServerFactory = new TomcatServletWebServerFactory();
tomcatServletWebServerFactory.setDocumentRoot(
new File(rootDoc));
return tomcatServletWebServerFactory;
}
}
在配置文件中,配置当前webapp文件夹所在位置:
tomcat.factory.doc.root=D:/project/myBlog/blog-web/src/main/webapp
方法3:
1.打开项目结构:
2.修改为如下结构,即可:
该方法仅针对当前项目为独立的项目,不属于其他项目的module。若当前项目为其他项目的module,则只能使用前两种方法。