作者各必备工具的版本如下:
- Tomcat:apache-tomcat-7.0.63 (下载链接)
- Java EE - Eclipse:Luna Service Release 1 v4.4.1 (下载链接)
- Spring:4.2.0.RELEASE (无须下载)
- JDK:1.7.0_67 (下载链接)
步骤 1
使用 Java EE - Eclipse 新建一 Dynamic Web Project。
步骤 2
输入项目名 bdp。
Target Runtime 选 Apache Tomcat 7.0(不要选 Apache Tomcat 6.0,7 以后才支持 Servlet 3.0)。
点击 Next > 按钮。
步骤 3
编辑默认的 Source folders。
默认的 Source folders 配置如下:
删除默认的,增加以下四个并修改默认的输出目录为 WebContent\WEB-INF\classes:
- src/main/java
- src/main/resources
- src/test/java
- src/test/resources
点击 Next > 按钮。
步骤 4
Configure web module settings 对话框勾选 Generate web.xml deployment descriptor 选项:
点击 Finish 按钮。
步骤 5
将新建好的项目转换为 Maven 项目以管理项目依赖。
Package Explorer 视图下右击 bdp 项目名 -> Configure -> Convert to Maven project。
步骤 6
添加以下 jar 依赖到项目:
我的 pom.xml 文件如下:
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>bdp</groupId>
- <artifactId>bdp</artifactId>
- <version>1.0.0</version>
- <packaging>war</packaging>
- <name>bdp</name>
- <description>Basic Data Platform</description>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <spring.version>4.2.0.RELEASE</spring.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aop</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>jstl</artifactId>
- <version>1.2</version>
- </dependency>
- <dependency>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- <version>1.1.3</version>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.1</version>
- <configuration>
- <source>1.7</source>
- <target>1.7</target>
- </configuration>
- </plugin>
- <plugin>
- <artifactId>maven-war-plugin</artifactId>
- <version>2.4</version>
- <configuration>
- <warSourceDirectory>WebContent</warSourceDirectory>
- <failOnMissingWebXml>false</failOnMissingWebXml>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
步骤 7
在 src/main/resources 目录下创建 Spring MVC Bean 配置文件 bdpmvc-servlet.xml。
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.0.xsd">
- <context:component-scan base-package="com.defonds.bdp.city.controller" />
- <bean id="viewResolver"
- class="org.springframework.web.servlet.view.UrlBasedViewResolver">
- <property name="viewClass"
- value="org.springframework.web.servlet.view.JstlView" />
- <property name="prefix" value="/WEB-INF/jsp/" />
- <property name="suffix" value=".jsp" />
- </bean>
- </beans>
以上 bdpmvc-servlet.xml 配置文件里,我们定义了一个 <context:component-scan> 标签。这一配置将使 Spring 去加载 com.defonds.bdp.city.controller 及其子包下的所有 @Component、@Controller、@Service、@Repository 等注解了的类。
此外,我们还定义了一个 viewResolver bean,它将给 ModelAndView 的视图加上 /WEB-INF/jsp/ 前缀以及 .jsp 后缀。比如我们的 CityController 类里返回了一个视图名为 welcome 的 ModelAndView 对象,它将被解析至路径 /WEB-INF/jsp/welcome.jsp。
步骤 8
将 Spring MVC 映射到 /WEB-INF/web.xml 文件。
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
- <display-name>bdp</display-name>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <servlet>
- <servlet-name>bdpmvc</servlet-name>
- <servlet-class>
- org.springframework.web.servlet.DispatcherServlet
- </servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/classes/bdpmvc-servlet.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>bdpmvc</servlet-name>
- <url-pattern>*.json</url-pattern>
- <url-pattern>*.html</url-pattern>
- </servlet-mapping>
- </web-app>
DispatcherServlet 初始化的时候将会读取 /WEB-INF/classes/ 下的 bdpmvc-servlet.xml 文件。
步骤 9
创建 Controller 类。
- 包:com.defonds.bdp.city.controller
- 类名:CityController.java
- /**
- * File Name:CityController.java
- *
- * Copyright Defonds Corporation 2015
- * All Rights Reserved
- *
- */
- package com.defonds.bdp.city.controller;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.servlet.ModelAndView;
- /**
- *
- * Project Name:bdp
- * Type Name:CityController
- * Type Description:
- * Author:Defonds
- * Create Date:2015-8-27
- * @version
- *
- */
- @Controller
- @RequestMapping("/city")
- public class CityController {
- @RequestMapping("/welcome")
- public ModelAndView helloWorld() {
- String message = "<br><div style='text-align:center;'>"
- + "<h3>********** Hello World, Spring MVC Tutorial</h3>This message is coming from CityController.java **********</div><br><br>";
- return new ModelAndView("welcome", "message", message);
- }
- }
步骤 10
创建 /WebContent/index.jsp 和 /WebContent/WEB-INF/jsp/welcome.jsp 视图文件。
/WebContent/index.jsp:
- <html>
- <head>
- <title>Spring MVC Tutorial Series by defonds.com</title>
- <style type="text/css">
- body {
- background-image: url('http://img.blog.csdn.net/20150827184458936');
- }
- </style>
- </head>
- <body>
- <br>
- <div style="text-align:center">
- <h2>
- Hey You..!! This is your 1st Spring MCV Tutorial..<br> <br>
- </h2>
- <h3>
- <a href="city/welcome.html">Click here to See Welcome Message... </a>(to
- check Spring MVC Controller... @RequestMapping("/city/welcome"))
- </h3>
- </div>
- </body>
- </html>
/WebContent/WEB-INF/jsp/welcome.jsp:
- <html>
- <head>
- <title>Spring MVC Tutorial by Defonds - Hello World Spring MVC
- Example</title>
- <style type="text/css">
- body {
- background-image: url('http://img.blog.csdn.net/20150827184458936');
- }
- </style>
- </head>
- <body>${message}
- <br>
- <br>
- </body>
- </html>
万事俱备,项目各要素如下图:
步骤 11
右击项目名 -> Run As -> Maven Build...
Goals 添加 clean install,点击 Apply 和 Run 按钮。
步骤 12
将项目部署到 Tomcat 并启动:
步骤 13
访问 http://localhost:8080/bdp
点击 Click here to See Welcome Message...
零基础搭建 spring mvc 4 项目成功。
后记
本文侧重讲 spring mvc 的相关配置,也就是 controller 层的相关功能演示,关于 spring ioc、orm 和 mybatis 的事务集成,请参考另一篇博客《零基础整合 spring 4(包括mvc、context、orm) + mybatis 3 示例》,该文就是在本文基础上进行进一步添加了 service 和 dao 层的扩展。