欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > javascript >内容正文

javascript

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

发布时间:2024/10/5 javascript 44 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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

总结

以上是生活随笔为你收集整理的Spring Boot 内置Tomcat——IntelliJ IDEA中配置模块目录设为文档根目录(DocumentRoot)解决方案的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。