大部分 Java 应用都是 Web 应用,展现层是 Web 应用不可忽略的重要环节。Spring 为展现层提供了一个优秀的 Web 框架—— Spring MVC。和众多其他 Web 框架一样,它基于 MVC 设计理念,此外,它采用了松散耦合可插拔组件结构,比其他 MVC 框架更具扩展性和灵活性。
SpringMVC + Mybatis 框架非常成熟,能够快速搭建项目。各大电商网站如京东、珍品网等都在使用这一框架组合。
本篇在前面“用Maven创建第一个项目”的代码基础上,将这个项目配置为支持SpringMVC框架。
将pom.xml的配置拷贝进去,更新maven引用包依赖:
注意:
1.从“<properties>
”开始一直到结尾的都可以直接拷,原来顶部的的项目信息可以保留:

<properties> <springside.version>4.2.2.GA</springside.version> <spring.version>4.2.5.RELEASE</spring.version> <mybatis.version>3.4.0</mybatis.version> <mybatis-spring.version>1.3.0</mybatis-spring.version> <logback.version>1.1.7</logback.version> <tomcat-jdbc.version>9.0.0.M4</tomcat-jdbc.version> <jackson.version>2.7.3</jackson.version> <!-- Plugin的属性定义 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <jdbc.driver.groupId>mysql</jdbc.driver.groupId> <jdbc.driver.artifactId>mysql-connector-java</jdbc.driver.artifactId> <jdbc.driver.version>6.0.2</jdbc.driver.version> </properties> <dependencies> <!-- JSON begin --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.module</groupId> <artifactId>jackson-module-jaxb-annotations</artifactId> <version>${jackson.version}</version> </dependency> <!-- JSON end --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</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-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis-spring.version}</version> </dependency> <!-- connection pool --> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> <version>${tomcat-jdbc.version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>${jdbc.driver.groupId}</groupId> <artifactId>${jdbc.driver.artifactId}</artifactId> <version>${jdbc.driver.version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.9</version> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>javax.mail-api</artifactId> <version>1.5.5</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.2</version> </dependency> <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.9.1</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.10</version> </dependency> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>javax.servlet.jsp.jstl-api</artifactId> <version>1.2.1</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> </dependencies> <build> <plugins> <!-- compiler插件, 设定JDK版本 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> <showWarnings>true</showWarnings> </configuration> </plugin> <!-- war打包插件, 设定war包名称不带版本号 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <warName>${project.artifactId}</warName> </configuration> </plugin> </plugins> </build> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>http://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories>
2.拷贝前可以到“http://mvnrepository.com”查询各个jar包的最新版本号:

3.保存pom.xml后,Eclipse会自动运行Building workspace从远程获取相应的jar包,耐心等待完成。
4.发现Markers下面提示了一个错误,可以用"Command + 1"进行修复:

5.现在多了很多jar包引用: