1.前言:
最近在学习SpringMVC框架,由于使用Eclipse创建的webAPP项目默认使用的还是比较旧的servlet2.3,而且默认使用的还是JDK1.5,所以便有一次开始了我的配置之路
2.新建Maven-webapp项目



很明显,我们新建的项目里面会有各种错误,比如很明显的JRE库是用的1.5,而我的是1.8,明显用的不是我的。先吐槽一句:难道只有我的eclipse这么倒霉?接下来就一个个的去解决问题吧。
问题1:Java resources目录不显示默认包名
解决方法:右键-->Build path-->configure Build path,remove掉 “excluded:**” 即可

操作之后便可看到 Java Resources 下的目录恢复正常

问题2:Javax包的缺失,JRE版本
这个问题一般在导入别人的项目的时候一般也会遇到,其解决方法是右键-->Build path-->configure Build path,添加对应的库即可。因为我的项目是用Maven来构建的,所以这个问题的解决就显得比较简单了
解决方法:
在pom.xml配置文件中添加对应的依赖库即可(我顺便把项目默认的Junit版本换为了4.12版本)
1<!-- Junit --> 2 <dependency> 3 <groupId>junit</groupId> 4 <artifactId>junit</artifactId> 5 <version>4.12</version> 6 </dependency> 7 <!-- javaEE begin--> 8 <dependency> 9 <groupId>javax</groupId> 10 <artifactId>javaee-api</artifactId> 11 <version>7.0</version> 12 </dependency> 13 <dependency> 14 <groupId>javax</groupId> 15 <artifactId>javaee-web-api</artifactId> 16 <version>7.0</version> 17 </dependency>
同时,在build标签下配置项目的运行环境:
1<build> 2 <finalName>springMVCTest</finalName> 3 <plugins> 4 <plugin> 5 <groupId>org.apache.maven.plugins</groupId> 6 <artifactId>maven-compiler-plugin</artifactId> 7 <configuration> 8 <source>1.8</source> 9 <target>1.8</target> 10 </configuration> 11 </plugin> 12 </plugins> 13 </build>
更新项目:右击项目-->Maven-->Update project, 然后可以发现,index.jsp 上的错误没有了,并且JRE也换成了我的1.8版本。

但是这时候我们发现,为啥项目还提示有错误?我们打开problem试图(如果没有,window-->show view --> others,搜索problem ,即可打开)

我们大体可以看出现在的问题是因为我们的项目需要servlet2.5或以上,这也就是我们接下来要做的。
3.配置成为servlet2.5 web项目
打开我们项目的web.xml配置文件,这明显是2.3版本的,现在已经不怎么使用了。
1<!DOCTYPE web-app PUBLIC 2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 3 "http://java.sun.com/dtd/web-app_2_3.dtd" > 4 5<web-app> 6 <display-name>Archetype Created Web Application</display-name> 7</web-app>
把下面的代码复制到你的web.xml ,把display标签中的内容换为你们自己项目的名称即可。
1<?xml version="1.0" encoding="UTF-8"?> 2<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 5 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 6 <display-name>webappName</display-name> 7 <welcome-file-list> 8 <welcome-file>index.html</welcome-file> 9 <welcome-file>index.htm</welcome-file> 10 <welcome-file>index.jsp</welcome-file> 11 <welcome-file>default.html</welcome-file> 12 <welcome-file>default.htm</welcome-file> 13 <welcome-file>default.jsp</welcome-file> 14 </welcome-file-list> 15</web-app>
打开该项目的navigator视图(window-->show view -->navigator),
修改如如下图的文件内容:

将<installed facet="jst.web" version="2.3"/>改为<installed facet="jst.web" version="2.5"/>重新Update project即可,run项目,可以看到如下内容说明配置成功。

4.配置springMVC项目
向我们的项目中添加Spring必要的包即可,为了便于项目的导入,我这里将所以的项目的依赖包全部列了出来,使用时直接粘贴到你的项目即可
1<properties> 2 <!-- springframe 版本控制 --> 3 <spring.version>4.3.0.RELEASE</spring.version> 4 </properties> 5 6 <dependencies> 7 <!-- Junit --> 8 <dependency> 9 <groupId>junit</groupId> 10 <artifactId>junit</artifactId> 11 <version>4.12</version> 12 </dependency> 13 <!-- javaEE begin--> 14 <dependency> 15 <groupId>javax</groupId> 16 <artifactId>javaee-api</artifactId> 17 <version>7.0</version> 18 </dependency> 19 <dependency> 20 <groupId>javax</groupId> 21 <artifactId>javaee-web-api</artifactId> 22 <version>7.0</version> 23 </dependency> 24 <!-- javaEE end --> 25 <!-- spring begin --> 26 <!-- spring 核心功能包 27 spring-aop-4.3.0.RELEASE.jar 28 spring-beans-4.3.0.RELEASE.jar 29 spring-context-4.3.0.RELEASE.jar 30 spring-core-4.3.0.RELEASE.jar 31 spring-expression-4.3.0.RELEASE.jar 32 commons-logging-1.2.jar 33 --> 34 <dependency> 35 <groupId>org.springframework</groupId> 36 <artifactId>spring-context</artifactId> 37 <version>${spring.version}</version> 38 </dependency> 39 40 <!-- spring 附加功能包 --> 41 <dependency> 42 <groupId>org.springframework</groupId> 43 <artifactId>spring-webmvc</artifactId> 44 <version>${spring.version}</version> 45 </dependency> 46 <dependency> 47 <groupId>org.springframework</groupId> 48 <artifactId>spring-tx</artifactId> 49 <version>${spring.version}</version> 50 </dependency> 51 <dependency> 52 <groupId>org.springframework</groupId> 53 <artifactId>spring-jdbc</artifactId> 54 <version>${spring.version}</version> 55 </dependency> 56 <dependency> 57 <groupId>org.springframework</groupId> 58 <artifactId>spring-test</artifactId> 59 <version>${spring.version}</version> 60 <scope>test</scope> 61 </dependency> 62 <!-- spring end --> 63 <!-- commons jar --> 64 <dependency> 65 <groupId>org.apache.commons</groupId> 66 <artifactId>commons-lang3</artifactId> 67 <version>3.3.2</version> 68 </dependency> 69 <dependency> 70 <groupId>commons-io</groupId> 71 <artifactId>commons-io</artifactId> 72 <version>2.4</version> 73 </dependency> 74 <dependency> 75 <groupId>org.apache.commons</groupId> 76 <artifactId>commons-collections4</artifactId> 77 <version>4.0</version> 78 </dependency> 79 <dependency> 80 <groupId>commons-logging</groupId> 81 <artifactId>commons-logging</artifactId> 82 <version>1.1.3</version> 83 </dependency> 84 <dependency> 85 <groupId>commons-codec</groupId> 86 <artifactId>commons-codec</artifactId> 87 <version>1.8</version> 88 </dependency> 89 <dependency> 90 <groupId>commons-beanutils</groupId> 91 <artifactId>commons-beanutils</artifactId> 92 <version>1.8.3</version> 93 </dependency> 94 <dependency> 95 <groupId>commons-chain</groupId> 96 <artifactId>commons-chain</artifactId> 97 <version>1.2</version> 98 </dependency> 99 <dependency> 100 <groupId>commons-fileupload</groupId> 101 <artifactId>commons-fileupload</artifactId> 102 <version>1.3.1</version> 103 </dependency> 104 <dependency> 105 <groupId>org.apache.commons</groupId> 106 <artifactId>commons-math3</artifactId> 107 <version>3.3</version> 108 </dependency> 109 <dependency> 110 <groupId>org.apache.commons</groupId> 111 <artifactId>commons-pool2</artifactId> 112 <version>2.2</version> 113 </dependency> 114 <dependency> 115 <groupId>org.apache.commons</groupId> 116 <artifactId>commons-digester3</artifactId> 117 <version>3.2</version> 118 </dependency> 119 <dependency> 120 <groupId>commons-net</groupId> 121 <artifactId>commons-net</artifactId> 122 <version>3.3</version> 123 </dependency> 124 <dependency> 125 <groupId>commons-dbutils</groupId> 126 <artifactId>commons-dbutils</artifactId> 127 <version>1.5</version> 128 </dependency> 129 <dependency> 130 <groupId>org.apache.commons</groupId> 131 <artifactId>commons-email</artifactId> 132 <version>1.3.3</version> 133 </dependency> 134 <dependency> 135 <groupId>commons-dbcp</groupId> 136 <artifactId>commons-dbcp</artifactId> 137 <version>1.4</version> 138 </dependency> 139 140 </dependencies>
项目的源代码已经传到我的Github,地址:https://github.com/Taoey/cnblog-demos/releases/tag/v1.0 在该页面搜索“springMVCTest”即可下载该项目”
参考博文:
[1] http://www.cnblogs.com/meet/p/6413050.html
[2] http://blog.csdn.net/gisredevelopment/article/details/2443806