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

原文章请点此处

启动时debug

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

public final File getValidDirectory() {
	File file = this.directory;
	file = (file != null) ? file : getWarFileDocumentRoot();
	file = (file != null) ? file : getExplodedWarFileDocumentRoot();
	file = (file != null) ? file : getCommonDocumentRoot();
	if (file == null && this.logger.isDebugEnabled()) {
		logNoDocumentRoots();
	}
	else if (this.logger.isDebugEnabled()) {
		this.logger.debug("Document root: " + file);
	}
	return file;
}

这段代码

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

其实是想找到 user.dir默认项目根路径下是否存在如下这几个文件夹,存在则返回根路径,这样就可以映射到具体文件夹下面。

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

但是如果多层级,此时打印System.getProperty(“user.dir”) 为project的根目录,当找不到这些文件夹,该方法返回null,映射失败才会导致404。
在这里插入图片描述
如图设置后 再次打印 System.getProperty(“user.dir”) 会发现此时路径为module的路径,这样就可以找到以上文件夹,正确映射后 就不会404了。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值