2019.10.10第一次手动编写基于ssm框架的项目经历
这是我第一次自主使用ssm框架编写项目代码,做的项目是网上常见的"黑马旅游网",刚开始我很急躁,以为自己可以随便写写就把注册页面后台代码实现,于是采用了不规范的开发方式(直接编写所有的代码并整合框架,没有分模块进行单元测试),成功的碰见了几个知识盲区,后来因此而老老实实的重新拆开框架进行单元测试排查问题,在此我记录了几个开发过程中碰到的几个坑来警示自己,同时也希望能给刚好也出现这些问题的朋友一个前车之鉴。
Error1.HTTP Status 500 - Servlet.init() for servlet springmvc threw exception
Error creating bean with name ‘userDao’: Unsatisfied dependency expre---------中间部分找不到了---------No qualifying bean of type ‘cn.itcast.travel.dao.IUserDao’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
以上是我拆完框架之后出现的问题,具体错误字段太长我只找回了当时搜索的这一部分,这是整合前端页面和web层的时候报的错误,我看了一下错误的原因好像是因为创建不了Dao和Service的实现类对象导致了Web层及Service层的注入都失败,由此顺藤摸瓜的想到在我测试完service层和dao层整合成功之后,仅剩的可能性是我写的一个配置文件可能产生在扫描包的时候的故障问题,但由于我的Service层测试成功得出配置文件没有问题,剩下最大的可能就在配置文件的读取上了,打开我的web.xml文件,发现果然因为我的Listener标签在拆分框架时被我注解了忘记打开,打开之后这个问题解决,出现下一个问题。
Error2.HTTP Status 404
上一个问题在最后一步调整完测试结果出现这个问题,这个我真的一点头绪也没有,我复盘了一下我的代码,觉得没什么语法和拼写上的错误,唯一不确定的是写的springmvc.xml中的视图解析器的配置,因为之前写的都是jsp页面,这次是都html页面,我就照葫芦画瓢的直接把视图解析器的.jsp改成了.html,我猜想可能是由于我的编写不规范导致找不到资源文件,就百度了一下跳转html页面的操作,发现了这篇帖子写的方法:https://blog.csdn.net/just_give_up/article/details/98775300,发现我写的和他写的对比之下唯一的区别是配置静态资源的那一句
<mvc:default-servlet-handler/>,加上之后果然问题解决,页面可以成功访问。
ps: 此项目的前台代码由网课附带,可直接导入IDEA,常见问题还有用Tomcat加载的页面css和js失效,将pom.xml中的servlet-api的2.*版本改为javax-servlet-api的3.*版本即可