搭建Spring MVC 4开发环境八步走
Spring MVC作为SpringFrameWork的产品,自诞生之日,就受到广泛开发者的关注,如今Spring MVC在Java中的发展可谓是蒸蒸日上,如今如果再有开发者说,不了解Spring MVC,或许就被人笑掉大牙。煽情的话就不说了,先告诉大家如何搭建Spring MVC开发环境。
(一)工作环境准备:
JDK 1.7
Eclipse Kepler
Apache Tomcat 8.0
(二)在Eclipse中新建Maven工程,在Archetype类型中,选择“maven-archetype-webapp”。
(三)配置pom.xml。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
< 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/maven-v4_0_0.xsd" >
< modelVersion >4.0.0</ modelVersion >
< groupId >com.favccxx.favsoft</ groupId >
< artifactId >favspringmvcrestful</ artifactId >
< packaging >war</ packaging >
< version >0.0.1-SNAPSHOT</ version >
< name >favspringmvcrestful Maven Webapp</ name >
< url >http://maven.apache.org</ url >
< properties >
< spring.version >4.1.1.RELEASE</ spring.version >
</ properties >
< dependencies >
< dependency >
< groupId >junit</ groupId >
< artifactId >junit</ artifactId >
< version >3.8.1</ version >
< scope >test</ scope >
</ dependency >
< dependency >
< groupId >org.springframework</ groupId >
< artifactId >spring-core</ 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-beans</ artifactId >
< version >${spring.version}</ version >
</ dependency >
< dependency >
< groupId >org.springframework</ groupId >
< artifactId >spring-context</ artifactId >
< version >${spring.version}</ version >
</ dependency >
< dependency >
< groupId >jstl</ groupId >
< artifactId >jstl</ artifactId >
< version >1.2</ version >
</ dependency >
< dependency >
< groupId >taglibs</ groupId >
< artifactId >standard</ artifactId >
< version >1.1.2</ version >
</ dependency >
</ dependencies >
< build >
< finalName >favspringmvcrestful</ finalName >
</ build >
</ project >
|
(四)在WEB-INF/web.xml,配置Spring MVC转发。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
<? xml version = "1.0" encoding = "UTF-8" ?>
< web-app version = "2.4" xmlns = "http://java.sun.com/xml/ns/j2ee"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
< display-name >favspringmvcrestful</ display-name >
< filter >
< filter-name >encodingFilter</ filter-name >
< filter-class >org.springframework.web.filter.CharacterEncodingFilter</ filter-class >
< init-param >
< param-name >encoding</ param-name >
< param-value >UTF-8</ param-value >
</ init-param >
< init-param >
< param-name >forceEncoding</ param-name >
< param-value >true</ param-value >
</ init-param >
</ filter >
< filter-mapping >
< filter-name >encodingFilter</ filter-name >
< url-pattern >/*</ url-pattern >
</ filter-mapping >
< listener >
< listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class >
</ listener >
< servlet >
< servlet-name >springMVC</ servlet-name >
< servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class >
< init-param >
< param-name >contextConfigLocation</ param-name >
< param-value >classpath*:spring-context.xml</ param-value >
</ init-param >
< load-on-startup >1</ load-on-startup >
</ servlet >
< servlet-mapping >
< servlet-name >springMVC</ servlet-name >
< url-pattern >/</ url-pattern >
</ servlet-mapping >
</ web-app >
|
(五)在resources目录下,创建spring-context.xml,支持注解,页面路径解析等。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<? 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:context = "http://www.springframework.org/schema/context"
xmlns:mvc = "http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
< context:component-scan base-package = "com.favccxx.favsoft.favjson.controller" ></ context:component-scan >
< mvc:annotation-driven ></ mvc:annotation-driven >
< 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/views" />
< property name = "suffix" value = ".jsp" />
</ bean >
</ beans >
|
(六)新建HelloController类,使用注解完成Spring MVC类的调用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.favccxx.favsoft.favjson.controller;
import java.util.HashMap;
import java.util.Map;
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 HelloController {
@RequestMapping ( "/greeting" )
public ModelAndView greeting( @RequestParam (value= "name" , defaultValue= "World" ) String name) {
System.out.println( "Hello " + name);
Map<String, Object> map = new HashMap<String, Object>();
map.put( "userName" , name);
return new ModelAndView( "/hello" ,map);
}
} |
(七)创建/WEB-INF/views/hello.jsp,用来展现数据。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> < html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" >
< title >Hello</ title >
</ head >
< body >
你好,${userName }
</ body >
</ html >
|
(八)在浏览器中,输入访问的URL:http://localhost:8080/favspringmvcrestful/greeting?name=%E7%BE%8E%E5%A5%B3,运行效果如下:
该例子仅仅是Spring MVC 4入门的一个简单例子,如果你想了解更多关于Spring MVC的信息,请持续关注本博客,同时欢迎读者留言评论。
欢迎大家访问我的个人网站 萌萌的IT人