利用Maven搭建第一个Spring测试项目
在开始之前请先配置好Spring和Maven。
- 配置Spring可以到Spring官方下载开发包。更详细的地址。
PS:利用官方发布的Spring依赖包可以手动管理jar包,但是在Spring 3.0.2的版本之后官方已经停止对依赖包的更新(可以在这里下载spring-3.0.2.依赖包),因此推荐舍弃手动管理,直接使用maven。 - 配置Maven可参考Maven的下载以及安装、关于使用eclipse自带的maven插件以及使用自己安装maven插件。
1 在Eclipse中利用Maven配置依赖包
1.打开Eclipse,新建一个Maven工程。






这时我们需要修改一个地方。







2.双击打开pom.xml,默认是Overview视图,可以点击下面的页签来切换页面。编辑pom.xml文件,导入需要的包,这里使用的Spring开发包为4.3.22版本,代码如下。
<!-- Spring依赖 -->
<!-- 1.Spring核心依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.22.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.22.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.22.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.22.RELEASE</version>
</dependency>
<!-- 2.Spring dao依赖 -->
<!-- spring-jdbc包括了一些如jdbcTemplate的工具类 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.22.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.22.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.22.RELEASE</version>
</dependency>
<!-- 3.Spring web依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.22.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.22.RELEASE</version>
</dependency>
<!-- 4.Spring 日志包 -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<!-- 5.Spring test依赖:方便做单元测试和集成测试 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.22.RELEASE</version>
</dependency>
如果你需要某个包但不知道它的配置代码怎么写的话,推荐一个网站 http://maven.outofmemory.cn/ 这个网站是专门的maven中央库查询,maven上有的库都可以在上面找到。
3.保存pom.xml文件后,Maven自动进行jar包管理。

2 修改webapp默认配置
一般情况下,maven所创建的webapp默认版本过老。比如默认生成的Dynamic Web Module 为2.3版本,而如今Tomcat 8和spring4.0.0以上版本都要求是3.0才行。同样的还有Java和JavaScript等等。
通常情况下,我们会右键点击项目名称,选择Properties,进入项目属性页面,左侧选择Project Facts,直接进行webapp的修改。

但是在maven所创建的这个项目里,系统却无法点击 Apply 和 OK,例如我们将Dynamic Web Module修改为3.0。

没有探寻原因,可能是一个bug。我们直接采用另一种办法来解决即可。

进入本项目目录下的.settings文件夹内,修改该目录下的三个配置文件内容:

在org.eclipse.jdt.core.prefs中,将1.5修改为1.8(当前jdk版本)。
在org.eclipse.wst.common.component中,将project-version="1.5.0"修改为project-version="1.8.0"。
在org.eclipse.wst.common.project.facet.core.xml中,将<installed facet="jst.web" version="2.3"/>修改为<installed facet="jst.web" version="2.3"/>3.0、<installed facet="java" version="1.5"/>修改为<installed facet="java" version="1.5"/>1.8。
保存修改过后的文件,然后右键项目名称,选择Properties,在左侧选择栏中选择Project Facts,可以看到配置已经修改成功。
3 一个最基本的Spring项目
Maven创建的web项目目录结构浅析可参考这里,下面开始搭建一个最基本的Spring项目。
1.配置web.xml

修改代码如下。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
2.在src/main/resourse下创建springContext.xml文件
注意和web.xml里面context-param标签下面的.xml文件名和这里新建的.xm文件名保持一致。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 搜索spring控件 -->
<context:component-scan base-package="com.test"></context:component-scan>
<!-- 视图页面配置 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
在springContext.xml中,base-package是指定spring控制器控件的包,prefix指定的是视图目录,被设置为/WEB-INF/views,即视图目录被放到WEB-INF下。suffix指定的是视图的扩展名。例如,"hellospring"视图,将被放到/WEB-INF/views/hellospring.jsp。
3.创建spring控制器。


代码如下。
package com.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloSpringController {
String message = "Welcome to Spring MVC!";
@RequestMapping("/hello")
public ModelAndView showMessage(@RequestParam(value = "name", required = false, defaultValue = "Spring") String name) {
ModelAndView mv = new ModelAndView("hellospring");//指定视图
mv.addObject("message", message);
mv.addObject("name", name);
return mv;
}
}
4.创建spring视图。项目的首页index.jsp代码如下。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring 4 MVC - HelloWorld Index Page</title>
</head>
<body>
<center>
<h2>Hello World</h2>
<h3>
<a href="hello?name=Jarvan">点击跳转</a>
</h3>
</center>
</body>
</html>
5.建立跳转页面。


代码如下。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring 4 MVC -HelloWorld</title>
</head>
<body>
<center>
<h2>Hello World</h2>
<h2>
${message} ${name} </h2>
</center>
</body>
</html>
至此,一个最基本的Spring工程已经全部配置完毕了。
4 运行结果


5 总结
Spring入门第0.5天。利用Maven搭建第一个Spring测试项目,成功。
注:项目代码来自怀英兄