700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Spring Boot 内置Tomcat——IntelliJ IDEA中配置模块目录设为文档根目录(DocumentRoot)解决方案

Spring Boot 内置Tomcat——IntelliJ IDEA中配置模块目录设为文档根目录(DocumentRoot)解决方案

时间:2020-02-25 10:44:15

相关推荐

Spring Boot 内置Tomcat——IntelliJ IDEA中配置模块目录设为文档根目录(DocumentRoot)解决方案

源码分析

org.springframework.boot.web.servlet.server.DocumentRoot

/*** Returns the absolute document root when it points to a valid directory, logging a* warning and returning {@code null} otherwise.* @return the valid document root*/final File getValidDirectory() {File file = this.directory;// If document root not explicitly set see if we are running from a war archivefile = (file != null) ? file : getWarFileDocumentRoot();// If not a war archive maybe it is an exploded warfile = (file != null) ? file : getExplodedWarFileDocumentRoot();// Or maybe there is a document root in a well-known locationfile = (file != null) ? file : getCommonDocumentRoot();if (file == null && this.logger.isDebugEnabled()) {logNoDocumentRoots();}else if (this.logger.isDebugEnabled()) {this.logger.debug("Document root: " + file);}return file;}

发现有三种取路径方式:

war包 getWarFileDocumentRoot

导出包 getExplodedWarFileDocumentRoot

文档 getCommonDocumentRoot

内置tomcat启动应该属于第三种。

private static final String[] COMMON_DOC_ROOTS = { "src/main/webapp", "public","static" };

private File getCommonDocumentRoot() {for (String commonDocRoot : COMMON_DOC_ROOTS) {File root = new File(commonDocRoot);if (root.exists() && root.isDirectory()) {return root.getAbsoluteFile();}}return null;}

百度得知 取的是

System.getProperty("user.dir")

相当于

File root = new File(System.getProperty("user.dir")+"src/main/webapp");

输出 System.getProperty("user.dir") 发现是项目层目录,而不是模块层目录

解决方案

方法一:启动项增加配置参数

方法二:Spring Boot 插件启动

参考文章

springboot 在idea多模块下 子模块的web项目用内置tomcat启动访问jsp报404

springboot内置Tomcat的getServletContext().getRealPath问题

java中getRealPath("/")和getContextPath()的区别

ServletContext.getRealPath 为 null

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。