用Eclipse建spring mvc项目(图解)
转载地址:http://blog.****.net/fulai0_0/article/details/42967481
最近工作需要快速了解spring mvc架构,于是在网上找了很多相关教程。说真的,百度出来的基本都一样,但是我却遇到各种奇奇怪怪的问题。当然,做完之后回头看,发现那些问题都不是问题,哈哈哈哈~主要还是不懂Java这些东西,遇到问题的时候就会觉得很难,各种查,但是百度的结果又是让人很无奈,不同的网址,相同的内容!
我参考了一个博客来建项目,网址是:http://limingnihao.iteye.com/blog/830409。按照这个博客的流程创建了项目,但是由于实际情况的不同,中间还是有一些不一样的。
下面我把我用到的相关软件版本列一下,虽然我不知道这些有什么关系,但是,还是列一下吧,哈哈哈~~
因为不知道列哪些,所以就把这些都截了个图,只知道要用jdk,maven,tomcat,和java编译器(如eclipse)
现在开始建项目
1、创建一个maven项目
图1
图2
图3(第一个复选框一定要框,反正我这里不选就是错~)
图4(Packaging选择war。反正我之前选jar就有问题了)
图5(红框中显示的不是我想要的,需要修改,右击它,选择“Build Path” ==>“ConfigureBuild Path”)
图6
图7(不知道为什么选这个,只是之前看同事选了这个。)
图8
图9
到这一步,已经算是建好了一个mave项目。我们现在来配置一些东西。
2、配置pom.xml(可以直接添加到pom.xml里面,配置一些七七八八的东西。junit、jstl、 spring包)
3、在webapp下添加一个文件夹“WEB-INF”,在文件夹下添加一个文件“web.xml”(下面的内容可以全部复制过去)
web.xml文件里有几个值得注意的地方,上面标红的“dispatcher0121”可以修改为你想要的字符串,但是这个三个地方一定要一样。[servlet-name]+“-servlet.xml”这个是spring配置文件的文件名的命名规则。
4、在“webapp/WEB-INF/”下再添加一个文件“dispatcher0121-servlet.xml”,这个文件就是上面说的东西(可以全部复制到新建的文件中)
dispatcher0121-servlet.xml文件中也有要注意的地方,"example0121"这个是接下来会用的包,名字可以随意,但是不能全部是数字。我不想说,我就是偷懒用数字,导致之前建的项目一直有问题。
图10
5、创建一个Controller层测试类(这个是什么东西,我也不知道。只知道怎么操作)
图11
按照这个操作,可以添加包也可以添加类,先添加类,要是发现在包那里没有写“example0121”,那就取消,然后添加一个包,包的名字是“example0121”。然后再创建一个叫“GeneralController”的类。
图12
然后将代码写好,像下面一样
6、编写jsp页面。在“webapp/WEB-INF/”下添加一个文件夹“views”,再添加一个文件“index.jsp”。这个路径是在“dispatcher0121-servlet.xml”里面定下来的。
这个时候你可以看到你的工程出现红色的叉叉,报了个错,如图13。这个时候你要淡定,两种方法。一是无视它,二是删掉这个错。我选择了无视它,我因为这个错查了好久资料,莫名烦躁,试了网上说的方法,还是不行。最后问了同事,同事说,你运行一下可不可以用。我运行了项目,发现可以用。然后同事说,那就无视它。似乎好像是校验的问题,反正现在不影响。one or two?!
图13
7、运行
图14
图15
图16
图17
图18
现在算是创建好了项目。要开始深层次的探讨了