SpringMVC入门实例(解析工作原理)

来源:互联网 发布:建设银行mac版网银盾 编辑:程序博客网 时间:2024/05/02 01:58

环境准备

我们需要有基本的Java环境,下面只是简单的罗列,不再详细的介绍。

  • jdk1.6以上
  • eclipse或者myEclipse
  • tomcat6以上

我们使用SpringMVC的版本是Spring3.2.0,下图是最基本的jar包: 
这里写图片描述

在spring的官方API文档中,给出了所有jar包作用的概述,现列举常用的包以及相关作用:


  • org.springframework.aop-3.2.0.RELEASE.jar :与Aop 编程相关的包
  • org.springframework.beans-3.2.0.RELEASE.jar :提供了简捷操作bean 的接口
  • org.springframework.context-3.2.0.RELEASE.jar :构建在beans 包基础上,用来处理资源文件及国际化。
  • org.springframework.core-3.2.0.RELEASE.jar :spring 核心包
  • org.springframework.web-3.2.0.RELEASE.jar :web 核心包,提供了web 层接口
  • org.springframework.web.servlet-3.2.0.RELEASE.jar :web 层的一个具体实现包,DispatcherServlet也位于此包中。

后文全部实例都在spring3.2.0版本中进行,为了方便,建议在搭建环境中导入spring3.2.0 的所有jar 包(所有jar 包位于dist 目录下)。

环境准备好了,下面我们开始动手。

编写HelloWorld 实例

  1. 步骤一、建立名为SpringMVC_helloworld 的动态web项目,并选择服务器,并导入上面列出的jar 包。
  2. 步骤二、编写web.xml 配置文件,代码如下:
<?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/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">    <servlet>        <servlet-name>springMVC</servlet-name>        <servlet-class>            org.springframework.web.servlet.DispatcherServlet        </servlet-class>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>springMVC</servlet-name>        <url-pattern>*.do</url-pattern>    </servlet-mapping></web-app>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

简要说明:其实DispatcherServlet就是一个Servlet,也是对Http请求进行转发的核心Servlet 。所有.do的请求将首先被DispatcherServlet处理,而DispatcherServlet 它要作的工作就是对请求进行分发(也即是说把请求转发给具体的Controller )。可以简单地认为,它就是一个总控处理器,但事实上它除了具备总控处理理器对请求进行分发的能力外,还与spring 的IOC 容器完全集成在一起,从而可以更好地使用spring 的其它功能。在这里还需留意< servlet-name> springMVC ,下面步骤三会用到。 
3. 步骤三、建立Spring的配置文件,注意上一个步骤中的标签在web.xml中的servlet的名称。DispatcherServlet的初始化后,会在WEB - INF查找一个[servlet-name]-servlet.xml 即springMVC-servlet.xml的文件。它的主要代码如下:

<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd         http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-3.2.xsd         http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd         http://www.springframework.org/schema/tx         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">    <bean id="helloControl" class="com.tgb.controller.HelloWorld"></bean>    <!-- 处理器映射器 将bean的name作为url进行查找 ,需要在配置Handler时指定beanname(就是url)     所有的映射器都实现 HandlerMapping接口。    -->    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />    <!--简单url映射  -->    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">        <property name="mappings">            <props>                <!-- 对helloControl进行url映射,url是/helloworld.do -->                <prop key="/helloworld.do">helloControl</prop>            </props>        </property>    </bean>    <!-- 处理器适配器 所有处理器适配器都实现 HandlerAdapter接口 -->    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /></beans>
  • 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
  • 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

说明: helloworld.do的请求将被名为helloControl的bean 进行转发处理。 
4. 步骤四、创建一个SpringMVC的控制类,并处理请求,完成HelloWord.java 的编写,代码如下:

package com.tgb.controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;public class HelloWorld implements Controller{    @Override    public ModelAndView handleRequest(HttpServletRequest request,            HttpServletResponse response) throws Exception {        ModelAndView mav = new ModelAndView("hello.jsp");        mav.addObject("message", "Hello World!");        return mav;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

说明:ModelAndView 对象是包含视图和业务数据的混合对象,即通过此对象,我们可以知道所返回的相应页面(比如这里返回hello.jsp页面),也可以在相应的页面中获取此对象所包含的业务数据(比如这里message-Hello World!)。 
5.步骤五、创建jsp,在当前项目web根目录下编写index.jsp,用于发送请求,hello.jsp用于显示消息,主要代码如下: 
用index.jsp里面的超链接发出一个请求到HelloWorldController,并返回到hello.jsp 显示message的信息。

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body>    <a href="helloworld.do">SpringMVC,HelloWorld实例</a></body></html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body>    世界,你好!    获取值: ${message }</body></html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

6.步骤六:把项目扔到服务器中,运行服务器,在浏览器中输入http://localhost:8080/SpringMVC_HelloWorld/index.jsp 进行测试。 
这里写图片描述

这里写图片描述

简析springmvc 工作原理

  1. 启动服务器,根据web.xml的配置加载前端控制器(也称总控制器) DispatcherServlet 。在加载时会完成一系列的初始化动作。
  2. 根据servlet的映射请求(上面的HelloWorld实例中针对.do 请求),并参照“控制器配置文件”(即springMVC-servlet.xml 这样的配置文件),把具体的请求分发给特定的后端控制器进行处理(比如上例会分发给HelloWorld 控制器进行处理)
  3. 后端控制器调用相应的逻辑层代码,完成处理并返回视图对象( ModelAndView )给前端处理器。
  4. 前端控制器根据后端控制器返回的ModelAndView 对象,前端控器器根据视图对象返回具体页面给客户端。

总结

SpringMVC框架的核心是DispatcherServlet,它的作用是将请求分发给不同的后端处理器。Spring的Controller层使用了后端控制器来映射处理器和视图解析器来共同完成Controller层的主要工作。并且spring的Controller层还真正地把业务层处理的数据结果和相应的视图封装成一个对象,即我们后面会经常用到的ModelAndView 对象。

一句话总结springMVC

封装web请求为一个数据对象、调用业务逻辑层来处理数据对象、返回处理数据结果及相应的视图给用户。

0 0
原创粉丝点击