欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringMVC底層執(zhí)行流程及原理解析

 更新時(shí)間:2020年05月21日 09:07:58   作者:努力學(xué)習(xí)的Peanut  
這篇文章主要介紹了SpringMVC底層執(zhí)行流程及原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

一個(gè)簡單的HelloSpringMVC程序

先在web,xml中注冊一個(gè)前端控制器(DispatcherServlet)

 <?xml version="1.0" encoding="UTF-8"?>
 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
      version="4.0">
     <!--配置DispatcherServlet:這是SpringMVC的核心,請求分發(fā)器,前端控制器-->
     <servlet>
      <servlet-name>springmvc</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <!--要綁定springmvc的配置文件-->
       <init-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>classpath:springmvc-servlet.xml</param-value>
       </init-param>
       <!--啟動(dòng)級別為1-->
       <load-on-startup>1</load-on-startup>
     </servlet>
   <servlet-mapping>
     <servlet-name>springmvc</servlet-name>
     <url-pattern>/</url-pattern>
   </servlet-mapping>
 </web-app>

配置文件(springmvc-servlet.xml)

HandlerMapper是處理器映射器-->根據(jù)請求的地址去找處理器(如案例中的"/hello")

HandlerAdapter是處理器適配器-->找到處理器后根據(jù)id去適配對應(yīng)的controller(如適配到案例中的HelloController),controller會(huì)返回ModelAndView及其前端數(shù)據(jù)

ViewResolver是視圖解析器,其作用為:

1.獲取到ModelAndView中的數(shù)據(jù)

2.解析視圖名稱

3.拼接視圖名稱

4.數(shù)據(jù)渲染

 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
     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"> 
   <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
   <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> 
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
     <property name="prefix" value="/WEB-INF/jsp/"/>
     <property name="suffix" value=".jsp"/>
   </bean>
 
   <bean id="/hello" class="com.kuang.contorller.HelloController"/> 
 </beans>

controller層:

實(shí)現(xiàn)Controller接口,重寫內(nèi)部方法(一般不會(huì)使用,這是底層原理)

ModelAndView是模型、視圖

 public class HelloController implements Controller {
   @Override
   public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
     ModelAndView mv = new ModelAndView();
     mv.addObject("msg","HelloSpringMVC!");
     mv.setViewName("test");
     return mv;
   }
 }

底層流程圖

實(shí)線是SpringMVC已經(jīng)幫你實(shí)現(xiàn)好了,虛線是需要自己手動(dòng)

以上僅是說明底層執(zhí)行原理,實(shí)際開發(fā)并不會(huì)這樣去使用!

在實(shí)際開發(fā)中SpringMVC推薦使用注解的方式

在注解開發(fā)中,不需要我們?nèi)ヅ渲锰幚砥鬟m配器和處理器映射器。

web.xml中只需配置DispatcherServlet前端控制器

 <?xml version="1.0" encoding="UTF-8"?>
 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
      version="4.0">
   
   <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:springmvc-servlet.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>

在springmvc-servlet.xml中配置視圖解析器等

<context:component-scan base-package="com.kuang.controller"/>  /*組件掃描,用于掃描controller下的包*/
    <mvc:default-servlet-handler/>              /*靜態(tài)資源過濾*/
    <mvc:annotation-driven/>                 /*這個(gè)就幫助我們配置了映射器以及適配器*/
 <?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
     https://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    https://www.springframework.org/schema/mvc/spring-mvc.xsd">
     <context:component-scan base-package="com.kuang.controller"/>
     <mvc:default-servlet-handler/>
     <mvc:annotation-driven/> 
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
       <property name="prefix" value="/WEB-INF/jsp/"/>
       <property name="suffix" value=".jsp"/>
     </bean>
 </beans>

contorller

 @Controller    //說明這類被Spring托管了
 @RequestMapping("/hello")
 public class HelloController {
   @RequestMapping("/h1")   //這個(gè)注解會(huì)執(zhí)行視圖解析器
   public String hello(Model model){
     model.addAttribute("msg","helloSpringMVCAnnotation");
     return "hello";
   }
 }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 在logback.xml中自定義動(dòng)態(tài)屬性的方法

    在logback.xml中自定義動(dòng)態(tài)屬性的方法

    這篇文章主要介紹了在logback.xml中自定義動(dòng)態(tài)屬性的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-08-08
  • mac下修改idea的jvm運(yùn)行參數(shù)解決idea卡頓的情況

    mac下修改idea的jvm運(yùn)行參數(shù)解決idea卡頓的情況

    這篇文章主要介紹了mac下修改idea的jvm運(yùn)行參數(shù)解決idea卡頓的情況,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • java字符流緩沖區(qū)詳解

    java字符流緩沖區(qū)詳解

    這篇文章主要為大家詳細(xì)介紹了java字符流緩沖區(qū)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • 使用spring oauth2框架獲取當(dāng)前登錄用戶信息的實(shí)現(xiàn)代碼

    使用spring oauth2框架獲取當(dāng)前登錄用戶信息的實(shí)現(xiàn)代碼

    這篇文章主要介紹了使用spring oauth2框架獲取當(dāng)前登錄用戶信息的實(shí)現(xiàn)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Mybatis collection查詢集合屬性報(bào)錯(cuò)的解決方案

    Mybatis collection查詢集合屬性報(bào)錯(cuò)的解決方案

    這篇文章主要介紹了Mybatis collection查詢集合屬性報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java文本編輯器實(shí)現(xiàn)方法詳解

    Java文本編輯器實(shí)現(xiàn)方法詳解

    這篇文章主要介紹了Java文本編輯器實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了java文本編輯器結(jié)構(gòu)、原理、布局、實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2019-03-03
  • MyBatis 如何獲取子類的屬性

    MyBatis 如何獲取子類的屬性

    這篇文章主要介紹了MyBatis 如何獲取子類的屬性,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java中的Semaphore如何使用

    Java中的Semaphore如何使用

    Semaphore實(shí)際上是一種共享鎖,因?yàn)樗试S多個(gè)線程并發(fā)獲取共享的資源,在Semaphore對象創(chuàng)建時(shí)必須設(shè)置可用令牌的初始數(shù)量permits,用于控制并發(fā)時(shí)同時(shí)獲取資源權(quán)限的線程數(shù)量,這篇文章主要介紹了Java中的Semaphore如何使用,需要的朋友可以參考下
    2022-06-06
  • 從零搭建Spring Boot腳手架整合OSS作為文件服務(wù)器的詳細(xì)教程

    從零搭建Spring Boot腳手架整合OSS作為文件服務(wù)器的詳細(xì)教程

    這篇文章主要介紹了從零搭建Spring Boot腳手架整合OSS作為文件服務(wù)器的詳細(xì)教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Spring事務(wù)失效之常見場景分析

    Spring事務(wù)失效之常見場景分析

    這篇文章主要介紹了Spring事務(wù)失效之常見場景,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04

最新評論