SpringMVC基礎:運行原理流程圖及實現,controller接口實現,注解實現


前言:dispatcherservlet里包含handlermapping和handleradapter兩個處理器

    dispatcherservlet位置在所有請求之前的,也就是說所有請求都要通過dispatcherservlet來處理,這也是springMVC的核心

    handlerMapping作用:根據求情中到相應的handler(controller)

    handleradapter作用:去適配(找到)並執行對應handler(controller)的方法,如處理方法參數、相關注解、數據綁定、消息轉換、返回值、調用視圖解析器等等。

     

MVC:Model(dao,service) View(jsp)  Controller(servlet)

     模型           視圖        控制器   

一,web開發的兩個發展階段

 

 

 

二,SpringMVC流程圖(官方中文版)

 

 

 

 

 實際上(具體流程)SpringMVC流程圖

注意:實線部分Spring已經完成的

   我們只需要是實現虛線部分    

 

 

三,SpringMVC的簡單搭建 (底層實現)

  1.導入MVC依賴(添加web支持)

   使用Idea做web開發時,注意:別忘了在項目結構中的 artifacts/WERB-NF下創建一個lib,並將依賴jar包全部導入其中

  2.配置web.xml,注冊DispatcherServlet

    DispatcherServlet,是servlet上一層,對用戶和servlet之間進行接收或轉發(分發的作用),是SpringMVC的特有的,

    因為有封裝了一層,是程序更加簡易

<?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">

    <!--1.注冊DispatcherServlet-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--關聯一個springmvc的配置文件:【servlet-name】-servlet.xml-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <!--啟動級別-1-->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!--/ 匹配所有請求:(不包括,jsp)-->
    <!--/* 匹配所有請求:(包括,jsp)-->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

  注意:/和/*的區別
  答:/:只匹配所有請求,不會去匹配jsp頁面

    /*:匹配所有請求,會去匹配jsp頁面

  3,創建控制器(類)

package com.king.controller;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloController implements Controller {
    public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {

        //ModelAndView 模型和視圖
        ModelAndView mv = new ModelAndView();

        //封裝對象,放在ModelAndView
        mv.addObject("msg","HelloSpringMVC!");

        //封裝要跳轉的視圖,放在ModelAndView中
        mv.setViewName("hello");//:  /WEB-INF/jsp/hello.jsp

        return mv;
    }
}

 

 

  4,創建bean配置文件(Application.xml)

   作用:將對應類導入springIOC容器中

<?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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://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/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--處理映射器(url處理器)-->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

    <!--處理器適配器(url適配器)-->
    <!--視圖解析器-->
    <!--視圖解析器:DispatcherServlet給他的ModelAndView-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <!--前綴-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--后綴-->
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--Handler-->
    <bean id="/hello" class="com.king.controller.HelloController"/>

</beans>

  

SpringMVC工作流程小結:1,用戶操作,生成一個請求(servlet)

            2.這個servlet進入到SpringMVC的DispatcherServlet(請求分發器),通過映射處理器(BeanNameUrlHandlerMapping)的execution方法解析,獲得參數,返回給請求分發器,

            3.DispatcherServlet在將信息傳給url/映射適配器,通過它去找相對應controller,控制層的接口實現類,

            4.controller將已經封裝好的model和view傳回給適配器(HandlerAdapter),適配器再傳給dispatcherServlet

            5.最后跳轉頁面,並將model部署到頁面中實現后台與用戶的交互

 

 

四,注解實現 

 

web映射

<?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>

 

Spring IOC容器

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://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/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--指定要掃描的包,這個包下的注解就會生效-->
    <context:component-scan base-package="com.king.controller"/>

    <!--讓springmvc不處理靜態資源 .css .js  .mp3  .mp4-->
    <mvc:default-servlet-handler/>

    <!--
        支持mvc注解驅動
            在spring中一般采用@RequestMapping,如果向@RequestMapping生效,
            必須向上下文中注冊DefaultAnnotationHandleMapping,映射處理器(url處理)
            和以一個AnnotationMethodHandleAdapter,
            這兩個一個是作用到類上,一個作用到方法上去處理
            而annotation-driver配置自動幫助我們進行屬性注入操作
    -->
    <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>

 

Controller模擬控制器接口

package com.king.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;



@Controller

public class HelloController {

    //請求映射器
    @RequestMapping("/h1")
    public String hello(Model model){
        //封裝數據
        model.addAttribute("msg","hello,springmvc");

        return "hello";//頁面跳轉


    }

}

@Controller:作用在類上,說明將當前類交給Spring容器托管

@RequestMapping 請求映射器

 

約束配置

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <!--將java代碼目錄中的xml輸出,默認不輸出除resources目錄外的xml文件,-->
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

 

依賴

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

 

 

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM