项目基础结构(项目地址 : 项目地址:https://github.com/luocaca/models.git )
--
现在我们讲一讲 jsp 引入过程中404搞得你措手不及
本次排查解决的几个问题:
1. 引入jsp出现404 去源码中排查
2. thymeleaf 与 jsp 共存问题
--前排申明-- (不然怕你们项目跑起来不会登陆)
username:root
password:root

一.(jsp引入前准备)
web项目引入,本文以 model-web 模块进行展开

- 新建一个model-web ,这个就不说了,右键models,命名 model-web,本model就是这么来的 ,O(∩_∩)O哈哈~

- 手动建一个 webapp.WEB-INF.jsp.index.jsp 文件

- 配置model的 project Structure (译文:项目结构)

-或者

-

- 配置完webapp 目录与 web.xml 就能够让项目顺利找到 webapp 下边的文件了
二 (jsp 引入相关包 maven 包)
--

-- jsp 配置文件,设置前缀,后缀,源目录在webapp ,刚刚已经设置过一次了

三(配置完毕,开跑.... run 起来 8082端口 )

接下来 open in brower
访问 toIndexJsp

进入断点,继续跑一下

搜嘎,咋回事,404白板,看看文件目录,

/WEB-INF/jsp/index.jsp
没毛病啊,对应的位置也是有的啊。什么鬼,没辙,找原因呗。
咋办,想不出哪里有问题 ,问问度娘吧。

看了几篇,大概可以定位到问题所在,
1.修改一下运行配置

新增一个配置 ,这个是我的 项目位置,你们要改成你们项目所在位置(D:\android_luocaca\idea_workpase\models\model-web)model的绝对路径


- re run 重新跑一个

ok 跑起来了,大吉大利,百度老铁,一个字,稳妥。,,,哈哈哈
四 (为啥配置了个路径就没事了,刚才还有问题的。想想,还是想不通,估计是绝对路径跟相对路径的问题吧)
但是我还想找找具体原因 404 哪里产生的?
- 404 产生的原因
(*^▽^*),我问问百度,

好多啊,一两天看不完,
-- 我们知道spring boot 嵌入内置的tomcat 然后通过tomcat 的初始化,将jsp 放到tomcat 容器里面,最后通过spring boot 动态代理去 根据


再插入中间 index 组合成 /WEB-INF/JSP/index.jsp
然后通过这个路径去找到 这个图中的位置

本应该没问题的,但是就是404 白板了,擦 ,咋们现在去tomcat 里面看看到底为什么 找不到了,,,,
五 (找到启动类,断点打一个,咋们看看)
org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory

tomcat 创建类 ,一个生产者, 提供

看到一个prepareContext(tomcat.getHost(), initializers);
准备上下文,打个断点,咋们跟踪一下,里面代码到底怎么写的。嘿嘿。有点多。看得很懵逼。。。。。

.耐
.心
.等
.待
runing ....

看到没。汤姆猫 内置spring以后,没处安放,只能偷偷在c盘user下边偷偷建一个临时路径,在里面偷偷跑起来,...嘿嘿嘿

继续走起。

找到了prepareContext 看看上下文环境都做了些什么

f7 继续跟进去看看






说明当前路径有效,也就是我们刚才设置了

现在能够找到路径位置
现在让我们看看 root 的绝对位置是什么
alt + F8

没毛病,跟我们working dir 设置的位置一样,搜嘎,百度给的答案真的挺准
继续走起,走完断点,后边没什么可看的了,
跑一个


- 回头想想,那刚才怎么不行,那我把working dir 清掉,再看一看这个方法,
来来来,让我们重蹈覆辙



重debug run
嘿嘿嘿,猫哥。我又来抓你了



正常情况下,应该跟刚才一样 走 root.exists() 就是文件存在

文件不存在啊,咋们看看root 是 哪来的妖怪,
Alt + F8 root 参数
root.getAbsolutePath()
D:\android_luocaca\idea_workpase\models\public

我的天,我是model-web模块,你咋去models里面找了捏

继续走完断点

我在想一想,你是不是傻,难道要我把 src/webapp/WEB-INF/jsp/index.jsp
拷到models 下边嘛。你当我傻嘛 。我能那样干嘛。
诶。这想法好。我试试看。把 src/webapp/WEB-INF/jsp/index.jsp 复制到 models 对应的路径下。在试试。

现在我要把 src.main.webapp.web-inf.jsp.index.jsp
复制到models 的目录底下。

应该就有 需要的文件了。
咋们重新跑一个。
抓到汤姆猫以后
直接走到这里


那我怎么知道是 model-web还是models 父类的。
哈哈。还是看看绝对路径把。

用文件打开看看

过了断点,看看

我居然听了tomcat 的鬼话。把model-web的文件复制到models下边去,被别人看到。一定会笑话我的。
现在我把working dir 改回原来项目路径
D:\android_luocaca\idea_workpase\models\model-web

rerun ing
.稍
.等
.一
.下

.没毛病,现在可以愉快的玩耍jsp了
分割线
后面还有一点 thymeleaf 和jsp 要公用的
,下次补充了。已经写在demo里面,,也可以自己去看。
项目地址:https://github.com/luocaca/models.git

就能共存了
happy。
参考网站:www.baidu.com
参考文章:https://www.iflym.com/index.php/java-programe/201706060001.html
网友评论