SpringBoot 多模块项目访问不到 JSP 页面问题解决方式

背景

最近在开发过程中碰到了一个问题,哪就是项目做了多模块改造之后发现使用 SpringBoot 默认内嵌 Tomcat 服务器启动项目之后,存在接口可以正常访问但是唯独访问不到 JSP 页面的问题,困扰了很久,也找了很多资料,但是都没有很系统的做出归纳,在此写篇文章记录一下我的处理过程!
(P.S:打成war包到外面tomcat启动是没有问题。只是不服,发现这个奇葩的问题没找到原因心中不爽。)

问题分析

无法访问jsp,很自然想到:

  1. 路径有没有映射对?
  2. 文件是否存在。

检查一遍之后发现映射没有问题,文件也存在。这就比较奇葩了。唯有看一下 SpringBoot 在启动的时候如何定义 web root 的路径。跟一下 SpringBoot 的 tomcat 启动包的源码:

/**
 * 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
 */
protected final File getValidDocumentRoot() {
   File file = getDocumentRoot();
   // If document root not explicitly set see if we are running from a war archive
   file = file != null ? file : getWarFileDocumentRoot();
   // If not a war archive maybe it is an exploded war
   file = file != null ? file : getExplodedWarFileDocumentRoot();
   // Or maybe there is a document root in a well-known location
   file = file != null ? file : getCommonDocumentRoot();
   if (file == null && this.logger.isDebugEnabled()) {
      this.logger
            .debug("None of the document roots " + Arrays.asList(COMMON_DOC_ROOTS)
                  + " point to a directory and will be ignored.");
   }
   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 != null && root.exists() && root.isDirectory()) {
         return root.getAbsoluteFile();
      }
   }
   return null;
}

写死从上面配置的 3 个目录去取 doc 路径,看到这里问题就明了。关键是

File root = new File(commonDocRoot);
if (root != null && root.exists() && root.isDirectory()) {
   return root.getAbsoluteFile();
}

File root = new File("src/main/webapp") 的是 这个相对路径的前缀是取哪里的。
百度得知 取的是 System.getProperty("user.dir")
相当于File root = new File(System.getProperty("user.dir")+"src/main/webapp");

然后 debug 打印一下 System.getProperty("user.dir") 发现 是定位到了 父项目那层,而不是具体模块那层。

解决方法

既然发现了问题,解决就简单。
这里采用的是直接在启动项里面增加配置参数,将 user.dir 定位到模块里面,这个模块是存放 JSP 页面的模块。
在这里插入图片描述
问题解决。

最后:因为对 tomcat 的启动顺序不了解。跟这个过程的时候走了不少弯路。采用了最笨的方法,在后面层层倒推一直找到上面设置doc路径的方法。
如果熟悉原理,直接从开始就定位到那个方法就很快能解决了!
在此做个记录,避免自己或有遇到同样问题的人少走弯路。

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值