天天看點

Spring與web環境內建

Spring與web環境內建
Spring與web環境內建

1. Spring與Web環境內建

1.1 ApplicationContext應用上下文擷取方式

應用上下文對象是通過new ClasspathXmlApplicationContext(spring配置檔案) 方式擷取的,但是每次從容器中獲得Bean時都要編寫new ClasspathXmlApplicationContext(spring配置檔案) ,這樣的弊端是配置檔案加載多次,應用上下文對象建立多次。

在Web項目中,可以使用ServletContextListener監聽Web應用的啟動,我們可以在Web應用啟動時,就加載Spring的配置檔案,建立應用上下文對象ApplicationContext,在将其存儲到最大的域servletContext域中,這樣就可以在任意位置從域中獲得應用上下文ApplicationContext對象了。

1.2 Spring提供擷取應用上下文的工具

上面的分析不用手動實作,Spring提供了一個監聽器ContextLoaderListener就是對上述功能的封裝,該監聽器内部加載Spring配置檔案,建立應用上下文對象,并存儲到ServletContext域中,提供了一個用戶端工具WebApplicationContextUtils供使用者獲得應用上下文對象。

是以我們需要做的隻有兩件事:

①在web.xml中配置ContextLoaderListener監聽器(導入spring-web坐标)

②使用WebApplicationContextUtils獲得應用上下文對象ApplicationContext

1.3 導入Spring內建web的坐标

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.0.5.RELEASE</version>
</dependency>           

1.4 配置ContextLoaderListener監聽器

<!--全局參數-->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--Spring的監聽器-->
<listener>
    <listener-class>
       org.springframework.web.context.ContextLoaderListener
   </listener-class>
 </listener>           

1.5 通過工具獲得應用上下文對象

ApplicationContext applicationContext =    
    WebApplicationContextUtils.getWebApplicationContext(servletContext);
    Object obj = applicationContext.getBean("id");           

知識要點

Spring內建web環境步驟

​ ①配置ContextLoaderListener監聽器

​ ②使用WebApplicationContextUtils獲得應用上下文

2. SpringMVC的簡介

2.1 SpringMVC概述

SpringMVC 是一種基于 Java 的實作 MVC 設計模型的請求驅動類型的輕量級 Web 架構,屬于SpringFrameWork 的後續産品,已經融合在 Spring Web Flow 中。

SpringMVC 已經成為目前最主流的MVC架構之一,并且随着Spring3.0 的釋出,全面超越 Struts2,成為最優秀的 MVC 架構。它通過一套注解,讓一個簡單的 Java 類成為處理請求的控制器,而無須實作任何接口。同時它還支援 RESTful 程式設計風格的請求。

2.3 SpringMVC快速入門

需求:用戶端發起請求,伺服器端接收請求,執行邏輯并進行視圖跳轉。

開發步驟

①導入SpringMVC相關坐标

②配置SpringMVC核心控制器DispathcerServlet

③建立Controller類和視圖頁面

④使用注解配置Controller類中業務方法的映射位址

⑤配置SpringMVC核心檔案 spring-mvc.xml

⑥用戶端發起請求測試

代碼實作

①導入Spring和SpringMVC的坐标、導入Servlet和Jsp的坐标

<!--Spring坐标-->
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-context</artifactId>
     <version>5.0.5.RELEASE</version>
 </dependency>
 <!--SpringMVC坐标-->
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-webmvc</artifactId>
     <version>5.0.5.RELEASE</version>
 </dependency>
<!--Servlet坐标-->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
</dependency>
<!--Jsp坐标-->
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.0</version>
</dependency>           

②在web.xml配置SpringMVC的核心控制器

<servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>   
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
           

③建立Controller和業務方法

public class QuickController {
    public String quickMethod(){
        System.out.println("quickMethod running.....");
        return "index";
    }
}           

③建立視圖頁面index.jsp

<html>
<body>
    <h2>Hello SpringMVC!</h2>
</body>
</html>           

④配置注解

@Controller
public class QuickController {
    @RequestMapping("/quick")
    public String quickMethod(){
        System.out.println("quickMethod running.....");
            return "index";
    }
}           

⑤建立spring-mvc.xml

<beans xmlns="http://www.springframework.org/schema/beans"      xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:context="http://www.springframework.org/schema/context"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans.xsd     http://www.springframework.org/schema/mvc       http://www.springframework.org/schema/mvc/spring-mvc.xsd      http://www.springframework.org/schema/context       http://www.springframework.org/schema/context/spring-context.xsd">    <!--配置注解掃描-->    <context:component-scan base-package="com.itheima"/></beans>           

⑥通路測試位址

http://localhost:8080/itheima_springmvc1/quick            

控制台列印

Spring與web環境內建

頁面顯示

Spring與web環境內建

2.3 SpringMVC流程圖示

Spring與web環境內建

2.4 知識要點

SpringMVC的開發步驟

3. SpringMVC的元件解析

3.1 SpringMVC的執行流程

Spring與web環境內建

①使用者發送請求至前端控制器DispatcherServlet。

②DispatcherServlet收到請求調用HandlerMapping處理器映射器。

③處理器映射器找到具體的處理器(可以根據xml配置、注解進行查找),生成處理器對象及處理器攔截器(如果有則生成)一并傳回給DispatcherServlet。

④DispatcherServlet調用HandlerAdapter處理器擴充卡。

⑤HandlerAdapter經過适配調用具體的處理器(Controller,也叫後端控制器)。

⑥Controller執行完成傳回ModelAndView。

⑦HandlerAdapter将controller執行結果ModelAndView傳回給DispatcherServlet。

⑧DispatcherServlet将ModelAndView傳給ViewReslover視圖解析器。

⑨ViewReslover解析後傳回具體View。

⑩DispatcherServlet根據View進行渲染視圖(即将模型資料填充至視圖中)。DispatcherServlet響應使用者。

3.2 SpringMVC元件解析

  1. 前端控制器:DispatcherServlet

​ 使用者請求到達前端控制器,它就相當于 MVC 模式中的 C,DispatcherServlet 是整個流程控制的中心,由

它調用其它元件處理使用者的請求,DispatcherServlet 的存在降低了元件之間的耦合性。

  1. 處理器映射器:HandlerMapping

​ HandlerMapping 負責根據使用者請求找到 Handler 即處理器,SpringMVC 提供了不同的映射器實作不同的

映射方式,例如:配置檔案方式,實作接口方式,注解方式等。

  1. 處理器擴充卡:HandlerAdapter

​ 通過 HandlerAdapter 對處理器進行執行,這是擴充卡模式的應用,通過擴充擴充卡可以對更多類型的處理

器進行執行。

  1. 處理器:Handler

​ 它就是我們開發中要編寫的具體業務控制器。由 DispatcherServlet 把使用者請求轉發到 Handler。由

Handler 對具體的使用者請求進行處理。

  1. 視圖解析器:View Resolver

​ View Resolver 負責将處理結果生成 View 視圖,View Resolver 首先根據邏輯視圖名解析成實體視圖名,即具體的頁面位址,再生成 View 視圖對象,最後對 View 進行渲染将處理結果通過頁面展示給使用者。

  1. 視圖:View

​ SpringMVC 架構提供了很多的 View 視圖類型的支援,包括:jstlView、freemarkerView、pdfView等。最常用的視圖就是 jsp。一般情況下需要通過頁面标簽或頁面模版技術将模型資料通過頁面展示給使用者,需要由程式員根據業務需求開發具體的頁面

3.3 SpringMVC注解解析

@RequestMapping

作用:用于建立請求 URL 和處理請求方法之間的對應關系

位置:

​ 類上,請求URL 的第一級通路目錄。此處不寫的話,就相當于應用的根目錄

​ 方法上,請求 URL 的第二級通路目錄,與類上的使用@ReqquestMapping标注的一級目錄一起組成通路虛拟路徑

屬性:

​ value:用于指定請求的URL。它和path屬性的作用是一樣的

​ method:用于指定請求的方式

​ params:用于指定限制請求參數的條件。它支援簡單的表達式。要求請求參數的key和value必須和配置的一模一樣

例如:

​ params = {"accountName"},表示請求參數必須有accountName

​ params = {"moeny!100"},表示請求參數中money不能是100

1.mvc命名空間引入

命名空間:xmlns:context="http://www.springframework.org/schema/context"        xmlns:mvc="http://www.springframework.org/schema/mvc"限制位址:http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc.xsd           

2.

元件掃描

SpringMVC基于Spring容器,是以在進行SpringMVC操作時,需要将Controller存儲到Spring容器中,如果使用@Controller注解标注的話,就需要使用<context:component-scan base-package=“com.itheima.controller"/>進行元件掃描。

3.4 SpringMVC的XML配置解析

SpringMVC有預設元件配置,預設元件都是DispatcherServlet.properties配置檔案中配置的,該配置檔案位址org/springframework/web/servlet/DispatcherServlet.properties,該檔案中配置了預設的視圖解析器,如下:

org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver           

翻看該解析器源碼,可以看到該解析器的預設設定,如下:

REDIRECT_URL_PREFIX = "redirect:"  --重定向字首FORWARD_URL_PREFIX = "forward:"    --轉發字首(預設值)prefix = "";     --視圖名稱字首suffix = "";     --視圖名稱字尾           
  1. 視圖解析器

我們可以通過屬性注入的方式修改視圖的的前字尾

<!--配置内部資源視圖解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  <property name="prefix" value="/WEB-INF/views/"></property>  <property name="suffix" value=".jsp"></property></bean>           

3.5 知識要點

SpringMVC的相關元件

SpringMVC的注解和配置

請求映射注解:@RequestMapping

視圖解析器配置:

REDIRECT_URL_PREFIX = "redirect:"

FORWARD_URL_PREFIX = "forward:"

prefix = "";

suffix = "";