零基础搭建SpringMVC4项目

来源:互联网 发布:不能安装淘宝助理 编辑:程序博客网 时间:2024/05/22 12:51
转载地址http://blog.csdn.net/defonds/article/details/48031593

作者各必备工具的版本如下:
  1. Tomcat:apache-tomcat-7.0.63 (下载链接)
  2. Java EE - Eclipse:Luna Service Release 1 v4.4.1 (下载链接)
  3. Spring:4.2.0.RELEASE (无须下载)
  4. JDK:1.7.0_67 (下载链接)

步骤 1

使用 Java EE - Eclipse 新建一 Dynamic Web Project。
使用 Java EE - Eclipse 新建一 Dynamic Web Project

步骤 2

输入项目名 bdp
输入项目名 bdp
Target Runtime 选 Apache Tomcat 7.0(不要选 Apache Tomcat 6.0,7 以后才支持 Servlet 3.0)。
点击 Next > 按钮。

步骤 3

编辑默认的 Source folders。
默认的 Source folders 配置如下:
默认的 Source folders 配置
删除默认的,增加以下四个并修改默认的输出目录为 WebContent\WEB-INF\classes:
  • src/main/java
  • src/main/resources
  • src/test/java
  • src/test/resources
增加以下四个并修改默认的输出目录为 WebContent WEB-INF classes
点击 Next > 按钮。

步骤 4

Configure web module settings 对话框勾选 Generate web.xml deployment descriptor 选项:
Configure web module settings 对话框勾选 Generate web.xml deployment descriptor 选项
点击 Finish 按钮。

步骤 5

将新建好的项目转换为 Maven 项目以管理项目依赖。
Package Explorer 视图下右击 bdp 项目名 -> Configure -> Convert to Maven project。

步骤 6

添加以下 jar 依赖到项目:
添加以下 jar 依赖到项目
我的 pom.xml 文件如下:
[html] view plain copy
print?
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>bdp</groupId>  
  5.     <artifactId>bdp</artifactId>  
  6.     <version>1.0.0</version>  
  7.     <packaging>war</packaging>  
  8.     <name>bdp</name>  
  9.     <description>Basic Data Platform</description>  
  10.   
  11.     <properties>  
  12.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  13.         <spring.version>4.2.0.RELEASE</spring.version>  
  14.     </properties>  
  15.   
  16.     <dependencies>  
  17.         <dependency>  
  18.             <groupId>org.springframework</groupId>  
  19.             <artifactId>spring-context</artifactId>  
  20.             <version>${spring.version}</version>  
  21.         </dependency>  
  22.         <dependency>  
  23.             <groupId>org.springframework</groupId>  
  24.             <artifactId>spring-aop</artifactId>  
  25.             <version>${spring.version}</version>  
  26.         </dependency>  
  27.         <dependency>  
  28.             <groupId>org.springframework</groupId>  
  29.             <artifactId>spring-webmvc</artifactId>  
  30.             <version>${spring.version}</version>  
  31.         </dependency>  
  32.         <dependency>  
  33.             <groupId>org.springframework</groupId>  
  34.             <artifactId>spring-web</artifactId>  
  35.             <version>${spring.version}</version>  
  36.         </dependency>  
  37.   
  38.         <dependency>  
  39.             <groupId>javax.servlet</groupId>  
  40.             <artifactId>jstl</artifactId>  
  41.             <version>1.2</version>  
  42.         </dependency>  
  43.   
  44.         <dependency>  
  45.             <groupId>commons-logging</groupId>  
  46.             <artifactId>commons-logging</artifactId>  
  47.             <version>1.1.3</version>  
  48.         </dependency>  
  49.     </dependencies>  
  50.   
  51.     <build>  
  52.         <plugins>  
  53.             <plugin>  
  54.                 <artifactId>maven-compiler-plugin</artifactId>  
  55.                 <version>3.1</version>  
  56.                 <configuration>  
  57.                     <source>1.7</source>  
  58.                     <target>1.7</target>  
  59.                 </configuration>  
  60.             </plugin>  
  61.             <plugin>  
  62.                 <artifactId>maven-war-plugin</artifactId>  
  63.                 <version>2.4</version>  
  64.                 <configuration>  
  65.                     <warSourceDirectory>WebContent</warSourceDirectory>  
  66.                     <failOnMissingWebXml>false</failOnMissingWebXml>  
  67.                 </configuration>  
  68.             </plugin>  
  69.         </plugins>  
  70.     </build>  
  71. </project>  

步骤 7

在 src/main/resources 目录下创建 Spring MVC Bean 配置文件 bdpmvc-servlet.xml
[html] view plain copy
print?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.    
  7. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  8. http://www.springframework.org/schema/context   
  9. http://www.springframework.org/schema/context/spring-context-4.0.xsd">  
  10.    
  11.     <context:component-scan base-package="com.defonds.bdp.city.controller" />  
  12.    
  13.     <bean id="viewResolver"  
  14.         class="org.springframework.web.servlet.view.UrlBasedViewResolver">  
  15.         <property name="viewClass"  
  16.             value="org.springframework.web.servlet.view.JstlView" />  
  17.         <property name="prefix" value="/WEB-INF/jsp/" />  
  18.         <property name="suffix" value=".jsp" />  
  19.     </bean>  
  20. </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 文件。
[html] view plain copy
print?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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">  
  3.   <display-name>bdp</display-name>  
  4.   <welcome-file-list>  
  5.     <welcome-file>index.jsp</welcome-file>  
  6.   </welcome-file-list>  
  7.     
  8.       <servlet>  
  9.         <servlet-name>bdpmvc</servlet-name>  
  10.         <servlet-class>  
  11.             org.springframework.web.servlet.DispatcherServlet  
  12.         </servlet-class>  
  13.         <init-param>  
  14.             <param-name>contextConfigLocation</param-name>  
  15.             <param-value>/WEB-INF/classes/bdpmvc-servlet.xml</param-value>  
  16.         </init-param>  
  17.         <load-on-startup>1</load-on-startup>  
  18.     </servlet>  
  19.     <servlet-mapping>  
  20.         <servlet-name>bdpmvc</servlet-name>  
  21.         <url-pattern>*.json</url-pattern>  
  22.         <url-pattern>*.html</url-pattern>  
  23.     </servlet-mapping>  
  24. </web-app>  

DispatcherServlet 初始化的时候将会读取 /WEB-INF/classes/ 下的 bdpmvc-servlet.xml 文件。

步骤 9

创建 Controller 类。
  • 包:com.defonds.bdp.city.controller
  • 类名:CityController.java

[java] view plain copy
print?
  1. /** 
  2.  * File Name:CityController.java 
  3.  * 
  4.  * Copyright Defonds Corporation 2015  
  5.  * All Rights Reserved 
  6.  * 
  7.  */  
  8. package com.defonds.bdp.city.controller;  
  9.   
  10. import org.springframework.stereotype.Controller;  
  11. import org.springframework.web.bind.annotation.RequestMapping;  
  12. import org.springframework.web.servlet.ModelAndView;  
  13.   
  14. /** 
  15.  *  
  16.  * Project Name:bdp 
  17.  * Type Name:CityController 
  18.  * Type Description: 
  19.  * Author:Defonds 
  20.  * Create Date:2015-8-27 
  21.  * @version  
  22.  *  
  23.  */  
  24. @Controller  
  25. @RequestMapping("/city")  
  26. public class CityController {  
  27.     @RequestMapping("/welcome")  
  28.     public ModelAndView helloWorld() {  
  29.    
  30.         String message = "<br><div style='text-align:center;'>"  
  31.                 + "<h3>********** Hello World, Spring MVC Tutorial</h3>This message is coming from CityController.java **********</div><br><br>";  
  32.         return new ModelAndView("welcome""message", message);  
  33.     }  
  34. }  

步骤 10

创建 /WebContent/index.jsp/WebContent/WEB-INF/jsp/welcome.jsp 视图文件。

/WebContent/index.jsp

[html] view plain copy
print?
  1. <html>  
  2. <head>  
  3. <title>Spring MVC Tutorial Series by defonds.com</title>  
  4. <style type="text/css">  
  5. body {  
  6.     background-image: url('http://img.blog.csdn.net/20150827184458936');  
  7. }  
  8. </style>  
  9. </head>  
  10. <body>  
  11.     <br>  
  12.     <div style="text-align:center">  
  13.         <h2>  
  14.             Hey You..!! This is your 1st Spring MCV Tutorial..<br> <br>  
  15.         </h2>  
  16.         <h3>  
  17.             <a href="city/welcome.html">Click here to See Welcome Message... </a>(to  
  18.             check Spring MVC Controller... @RequestMapping("/city/welcome"))  
  19.         </h3>  
  20.     </div>  
  21. </body>  
  22. </html>  
/WebContent/WEB-INF/jsp/welcome.jsp

[html] view plain copy
print?
  1. <html>  
  2. <head>  
  3. <title>Spring MVC Tutorial by Defonds - Hello World Spring MVC  
  4.     Example</title>  
  5. <style type="text/css">  
  6. body {  
  7.     background-image: url('http://img.blog.csdn.net/20150827184458936');  
  8. }  
  9. </style>  
  10. </head>  
  11. <body>${message}  
  12.    
  13.     <br>  
  14.     <br>  
  15. </body>  
  16. </html>  

万事俱备,项目各要素如下图:
万事俱备,项目各要素

步骤 11

右击项目名 -> Run As -> Maven Build...
Goals 添加 clean install,点击 Apply 和 Run 按钮。
Goals 添加 clean install,点击 Apply 和 Run 按钮

步骤 12

将项目部署到 Tomcat 并启动:
将项目部署到 Tomcat 并启动

步骤 13

访问 http://localhost:8080/bdp
访问首页
点击 Click here to See Welcome Message... 
点击 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 层的扩展。

参考资料

  • http://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/
0 0