快速搭建Spring MVC 4开发环境

(一)工作环境准备:

        JDK 1.7

        Eclipse Kepler

        Apache Tomcat 8.0

 

    (二)在Eclipse中新建Maven工程,在Archetype类型中,选择“maven-archetype-webapp”。

 

    (三)配置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/maven-v4_0_0.xsd">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   <groupId>com.favccxx.favsoft</groupId>  
  5.   <artifactId>favspringmvcrestful</artifactId>  
  6.   <packaging>war</packaging>  
  7.   <version>0.0.1-SNAPSHOT</version>  
  8.   <name>favspringmvcrestful Maven Webapp</name>  
  9.   <url>http://maven.apache.org</url>  
  10.      
  11.    <properties>  
  12.       <spring.version>4.1.1.RELEASE</spring.version>  
  13.   </properties>  
  14.      
  15.   <dependencies>  
  16.     <dependency>  
  17.       <groupId>junit</groupId>  
  18.       <artifactId>junit</artifactId>  
  19.       <version>3.8.1</version>  
  20.       <scope>test</scope>  
  21.     </dependency>  
  22.        
  23.     <dependency>  
  24.         <groupId>org.springframework</groupId>  
  25.         <artifactId>spring-core</artifactId>  
  26.         <version>${spring.version}</version>  
  27.     </dependency>  
  28.     <dependency>  
  29.         <groupId>org.springframework</groupId>  
  30.         <artifactId>spring-webmvc</artifactId>  
  31.         <version>${spring.version}</version>  
  32.     </dependency>  
  33.     <dependency>  
  34.         <groupId>org.springframework</groupId>  
  35.         <artifactId>spring-beans</artifactId>  
  36.         <version>${spring.version}</version>  
  37.     </dependency>  
  38.     <dependency>  
  39.         <groupId>org.springframework</groupId>  
  40.         <artifactId>spring-context</artifactId>  
  41.         <version>${spring.version}</version>  
  42.     </dependency>  
  43.        
  44.     <dependency>  
  45.         <groupId>jstl</groupId>  
  46.         <artifactId>jstl</artifactId>  
  47.         <version>1.2</version>  
  48.     </dependency>  
  49.     <dependency>  
  50.         <groupId>taglibs</groupId>  
  51.         <artifactId>standard</artifactId>  
  52.         <version>1.1.2</version>  
  53.     </dependency>  
  54.        
  55.   </dependencies>  
  56.   <build>  
  57.     <finalName>favspringmvcrestful</finalName>  
  58.   </build>  
  59. </project>  

 

 

    (四)在WEB-INF/web.xml,配置Spring MVC转发。

 

[html]  view plain copy print ? 在CODE上查看代码片
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  5.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  6.     <display-name>favspringmvcrestful</display-name>  
  7.    
  8.     <filter>  
  9.         <filter-name>encodingFilter</filter-name>  
  10.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  11.         <init-param>  
  12.             <param-name>encoding</param-name>  
  13.             <param-value>UTF-8</param-value>  
  14.         </init-param>  
  15.         <init-param>  
  16.             <param-name>forceEncoding</param-name>  
  17.             <param-value>true</param-value>  
  18.         </init-param>  
  19.     </filter>  
  20.     <filter-mapping>  
  21.         <filter-name>encodingFilter</filter-name>  
  22.         <url-pattern>/*</url-pattern>  
  23.     </filter-mapping>  
  24.    
  25.     <listener>  
  26.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  27.     </listener>  
  28.    
  29.     <servlet>  
  30.         <servlet-name>springMVC</servlet-name>  
  31.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  32.         <init-param>  
  33.             <param-name>contextConfigLocation</param-name>  
  34.             <param-value>classpath*:spring-context.xml</param-value>  
  35.         </init-param>  
  36.         <load-on-startup>1</load-on-startup>  
  37.     </servlet>  
  38.     <servlet-mapping>  
  39.         <servlet-name>springMVC</servlet-name>  
  40.         <url-pattern>/</url-pattern>  
  41.     </servlet-mapping>  
  42. </web-app>  

 

 

    (五)在resources目录下,创建spring-context.xml,支持注解,页面路径解析等。

 

[html]  view plain copy print ? 在CODE上查看代码片
 
  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"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd  
  8.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">  
  9.            
  10.     <context:component-scan base-package="com.favccxx.favsoft.favjson.controller"></context:component-scan>  
  11.        
  12.     <mvc:annotation-driven></mvc:annotation-driven>  
  13.        
  14.     <bean id="viewResolver" 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/views" />  
  18.         <property name="suffix" value=".jsp" />  
  19.     </bean>  
  20. </beans>  



 

    (六)新建HelloController类,使用注解完成Spring MVC类的调用。

 

 

 

[java]  view plain copy print ? 在CODE上查看代码片
 
  1. package com.favccxx.favsoft.favjson.controller;  
  2.    
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.    
  6. import org.springframework.stereotype.Controller;  
  7. import org.springframework.web.bind.annotation.RequestMapping;  
  8. import org.springframework.web.bind.annotation.RequestParam;  
  9. import org.springframework.web.servlet.ModelAndView;  
  10.    
  11. @Controller  
  12. public class HelloController {  
  13.        
  14.     @RequestMapping("/greeting")  
  15.     public ModelAndView greeting(@RequestParam(value="name", defaultValue="World") String name) {  
  16.          System.out.println("Hello " + name);  
  17.          Map<String, Object> map = new HashMap<String, Object>();  
  18.          map.put("userName", name);  
  19.          return new ModelAndView("/hello",map);  
  20.     }  
  21.         
  22. }  



 

    (七)创建/WEB-INF/views/hello.jsp,用来展现数据。

 

[html]  view plain copy print ? 在CODE上查看代码片
 
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  
  4. <!DOCTYPE html>  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8. <title>Hello</title>  
  9. </head>  
  10. <body>  
  11.     你好,${userName }  
  12. </body>  
  13. </html>  



 

(八)在浏览器中,输入访问的URL: http://localhost:8080/favspringmvcrestful/greeting?name=%E7%BE%8E%E5%A5%B3,显示Hello SpringMVC,则说明Spring MVC开发框架搭建成功。

你可能感兴趣的:(spring mvc)