1. 程式人生 > >【SpringMVC架構】SpringMVC入門實例,解析工作原理(二)

【SpringMVC架構】SpringMVC入門實例,解析工作原理(二)

rip 業務邏輯層 popu 輸入 implement override article hide -i

上篇博文,我們簡單的介紹了什麽是SpringMVC。這篇博文。我們搭建一個簡單SpringMVC的環境,使用非註解形式實現一個HelloWorld實例,從簡單入手,逐步深入。

環境準備

我們須要有主要的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項目,並選擇server,並導入上面列出的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/j2ee http://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>

簡要說明:其實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>

說明: 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;
    }
}

說明: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>
<%@ 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>

6.步驟六:把項目扔到server中。執行server,在瀏覽器中輸入http://localhost:8080/SpringMVC_HelloWorld/index.jsp 進行測試。
技術分享

技術分享

簡析springmvc 工作原理

  1. 啟動server,依據web.xml的配置載入前端控制器(也稱總控制器) DispatcherServlet 。

    在載入時會完畢一系列的初始化動作。

  2. 依據servlet的映射請求(上面的HelloWorld實例中針對.do 請求),並參照“控制器配置文件”(即springMVC-servlet.xml 這種配置文件)。把詳細的請求分發給特定的後端控制器進行處理(比方上例會分發給HelloWorld 控制器進行處理)
  3. 後端控制器調用對應的邏輯層代碼,完畢處理並返回視圖對象( ModelAndView )給前端處理器。
  4. 前端控制器依據後端控制器返回的ModelAndView 對象。前端控器器依據視圖對象返回詳細頁面給client。

總結

SpringMVC框架的核心是DispatcherServlet,它的作用是將請求分發給不同的後端處理器。Spring的Controller層使用了後端控制器來映射處理器和視圖解析器來共同完畢Controller層的主要工作。

而且spring的Controller層還真正地把業務層處理的數據結果和對應的視圖封裝成一個對象,即我們後面會經經常使用到的ModelAndView 對象。

一句話總結springMVC

封裝web請求為一個數據對象、調用業務邏輯層來處理數據對象、返回處理數據結果及對應的視圖給用戶。

下篇博文。我們使用註解形式,並改進HelloWorld實例,敬請期待。

【SpringMVC架構】SpringMVC入門實例,解析工作原理(二)