搭建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”。


wKiom1RjUsXTEvpEAAJU88LY5z0840.jpg


    (三)配置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,运行效果如下:


wKiom1RjVN7ANY6wAACjCtLOZ0w950.jpg

    该例子仅仅是Spring MVC 4入门的一个简单例子,如果你想了解更多关于Spring MVC的信息,请持续关注本博客,同时欢迎读者留言评论。





本文转自 genuinecx 51CTO博客,原文链接:http://blog.51cto.com/favccxx/1575885,如需转载请自行联系原作者
目录
相关文章
|
24天前
|
JSON 前端开发 Java
解决Spring MVC中No converter found for return value of type异常
在Spring MVC开发中遇到`No converter found for return value of type`异常,通常是因缺少消息转换器、返回值类型不支持或转换器优先级配置错误。解决方案包括:1) 添加对应的消息转换器,如`MappingJackson2HttpMessageConverter`;2) 自定义消息转换器并实现`HttpMessageConverter`接口,设置优先级;3) 修改返回值类型为如`ResponseEntity`的合适类型。通过这些方法可确保返回值正确转换为响应内容。
35 1
|
7天前
|
前端开发 Java 应用服务中间件
Spring MVC常见面试题
Spring MVC常见面试题
6 0
|
21天前
|
数据采集 前端开发 Java
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
23 3
|
21天前
|
存储 前端开发 Java
会话锦囊:揭示Spring MVC如何巧妙使用@SessionAttributes
会话锦囊:揭示Spring MVC如何巧妙使用@SessionAttributes
14 1
|
21天前
|
前端开发 Java Spring
数据之桥:深入Spring MVC中传递数据给视图的实用指南
数据之桥:深入Spring MVC中传递数据给视图的实用指南
31 3
|
21天前
|
前端开发 Java 容器
家族传承:Spring MVC中父子容器的搭建与管理指南
家族传承:Spring MVC中父子容器的搭建与管理指南
26 3
|
21天前
|
前端开发 Java API
头头是道:揭示Spring MVC如何获取和处理请求头数据
头头是道:揭示Spring MVC如何获取和处理请求头数据
22 1
|
21天前
|
前端开发 Java API
饼干探秘:深入Spring MVC中获取Cookie数据的技术解析
饼干探秘:深入Spring MVC中获取Cookie数据的技术解析
18 3
|
21天前
|
前端开发 Java Spring
转换之术:解析Spring MVC中类型转换器的实际运用
转换之术:解析Spring MVC中类型转换器的实际运用
21 0
|
21天前
|
前端开发 Java Spring
参数解密:揭示Spring MVC请求参数处理的实际应用指南
参数解密:揭示Spring MVC请求参数处理的实际应用指南
26 1