[转载]快速搭建Spring MVC 4开发环境

时间:2023-03-08 18:17:35

(一)工作环境准备:

JDK 1.7

Eclipse Kepler

Apache Tomcat 8.0

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

(三)配置pom.xml。

  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. <properties>
  11. <spring.version>4.1.1.RELEASE</spring.version>
  12. </properties>
  13. <dependencies>
  14. <dependency>
  15. <groupId>junit</groupId>
  16. <artifactId>junit</artifactId>
  17. <version>3.8.1</version>
  18. <scope>test</scope>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework</groupId>
  22. <artifactId>spring-core</artifactId>
  23. <version>${spring.version}</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework</groupId>
  27. <artifactId>spring-webmvc</artifactId>
  28. <version>${spring.version}</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework</groupId>
  32. <artifactId>spring-beans</artifactId>
  33. <version>${spring.version}</version>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework</groupId>
  37. <artifactId>spring-context</artifactId>
  38. <version>${spring.version}</version>
  39. </dependency>
  40. <dependency>
  41. <groupId>jstl</groupId>
  42. <artifactId>jstl</artifactId>
  43. <version>1.2</version>
  44. </dependency>
  45. <dependency>
  46. <groupId>taglibs</groupId>
  47. <artifactId>standard</artifactId>
  48. <version>1.1.2</version>
  49. </dependency>
  50. </dependencies>
  51. <build>
  52. <finalName>favspringmvcrestful</finalName>
  53. </build>
  54. </project>

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

  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. <filter>
  8. <filter-name>encodingFilter</filter-name>
  9. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  10. <init-param>
  11. <param-name>encoding</param-name>
  12. <param-value>UTF-8</param-value>
  13. </init-param>
  14. <init-param>
  15. <param-name>forceEncoding</param-name>
  16. <param-value>true</param-value>
  17. </init-param>
  18. </filter>
  19. <filter-mapping>
  20. <filter-name>encodingFilter</filter-name>
  21. <url-pattern>/*</url-pattern>
  22. </filter-mapping>
  23. <listener>
  24. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  25. </listener>
  26. <servlet>
  27. <servlet-name>springMVC</servlet-name>
  28. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  29. <init-param>
  30. <param-name>contextConfigLocation</param-name>
  31. <param-value>classpath*:spring-context.xml</param-value>
  32. </init-param>
  33. <load-on-startup>1</load-on-startup>
  34. </servlet>
  35. <servlet-mapping>
  36. <servlet-name>springMVC</servlet-name>
  37. <url-pattern>/</url-pattern>
  38. </servlet-mapping>
  39. </web-app>

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

  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. <context:component-scan base-package="com.favccxx.favsoft.favjson.controller"></context:component-scan>
  10. <mvc:annotation-driven></mvc:annotation-driven>
  11. <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
  12. <property name="viewClass"
  13. value="org.springframework.web.servlet.view.JstlView" />
  14. <property name="prefix" value="/WEB-INF/views" />
  15. <property name="suffix" value=".jsp" />
  16. </bean>
  17. </beans>

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

  1. package com.favccxx.favsoft.favjson.controller;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestParam;
  7. import org.springframework.web.servlet.ModelAndView;
  8. @Controller
  9. public class HelloController {
  10. @RequestMapping("/greeting")
  11. public ModelAndView greeting(@RequestParam(value="name", defaultValue="World") String name) {
  12. System.out.println("Hello " + name);
  13. Map<String, Object> map = new HashMap<String, Object>();
  14. map.put("userName", name);
  15. return new ModelAndView("/hello",map);
  16. }
  17. }

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

  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开发框架搭建成功。