Eclipse中使用Maven新建 Servlet 2.5的 SpringMVC项目

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版本)

  <!-- Junit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
    <!-- javaEE begin-->
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
    </dependency>

同时,在build标签下配置项目的运行环境:

  <build>
    <finalName>springMVCTest</finalName>
        <plugins>  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-compiler-plugin</artifactId>  
                <configuration>  
                <source>1.8</source>  
                <target>1.8</target>  
                </configuration>  
            </plugin>  
        </plugins>  
  </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版本的,现在已经不怎么使用了。

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
</web-app>

 把下面的代码复制到你的web.xml ,把display标签中的内容换为你们自己项目的名称即可。 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>webappName</display-name>
    <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</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必要的包即可,为了便于项目的导入,我这里将所以的项目的依赖包全部列了出来,使用时直接粘贴到你的项目即可

  <properties>
    <!-- springframe 版本控制 -->
    <spring.version>4.3.0.RELEASE</spring.version>  
  </properties>
  
  <dependencies>
  <!-- Junit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
    <!-- javaEE begin-->
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
    </dependency>
    <!-- javaEE end -->
    <!-- spring begin -->
    <!-- spring 核心功能包  
        spring-aop-4.3.0.RELEASE.jar  
        spring-beans-4.3.0.RELEASE.jar  
        spring-context-4.3.0.RELEASE.jar  
        spring-core-4.3.0.RELEASE.jar  
        spring-expression-4.3.0.RELEASE.jar  
        commons-logging-1.2.jar  
    -->  
    <dependency>  
          <groupId>org.springframework</groupId>  
          <artifactId>spring-context</artifactId>  
          <version>${spring.version}</version>  
    </dependency>  
      
    <!-- spring 附加功能包 -->  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-webmvc</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-tx</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-jdbc</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-test</artifactId>  
        <version>${spring.version}</version>  
        <scope>test</scope>  
    </dependency> 
        <!-- spring end -->     
        <!-- commons jar -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.0</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.3</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.8.3</version>
        </dependency>
        <dependency>
            <groupId>commons-chain</groupId>
            <artifactId>commons-chain</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-math3</artifactId>
            <version>3.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-digester3</artifactId>
            <version>3.2</version>
        </dependency>
        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.3</version>
        </dependency>
        <dependency>
            <groupId>commons-dbutils</groupId>
            <artifactId>commons-dbutils</artifactId>
            <version>1.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-email</artifactId>
            <version>1.3.3</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>

  </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

本文首发于公众号:【豆仔gogo】
非原创文章请查看原作者文章。原创文章版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/hwtblog/p/8321413.html