zoukankan      html  css  js  c++  java
  • SpringMVC-简介和执行原理分析(一)

    什么是MVC

    MVC : 模型、视图、控制器 , 是一种软件设计规范,说明不是设计模式;

    本质:将业务逻辑 , 数据 , 显示 分离的方式来编写代码; 前后端分离;

    Model:数据模型,提供要展示的数据,一般我们都会把这两个分离开来 , 数据Dao,服务层Service。

    View :负责进行数据的渲染和展示;客户端想要看到的东西

    Controller:接收用户请求,交给Model处理,从Model更新后的数据或者结果,返回给前端。调度员

    什么是SpringMVC

    SpringMVC 是 Spring的一部分,是基于Java实现的MVC的轻量级Web框架.

    优点:简单,易学,轻量级,高效,基于请求和响应的MVC框架,约定优于配置,功能强大:RestFul、数据验证、格式化{json}、主题,本地化、异常处理......

    The DispatcherServlet

    SpringMVC框架围绕着DispatcherServlet(Servlet请求分发器)设计;

     

    SpringMVC的执行原理

    1、DispatcherServlet表示前置控制器,是整个SpringMVC的控制中心。用户发出请求,DispatcherServlet接收请求并拦截请求。

    我们假设请求的url为:

    http://localhost:8080/SpringMVC/hello

    如上url拆分成三部分:

    http://localhost:8080服务器域名

    SpringMVC部署在服务器上的web站点

    hello表示控制器

    通过分析,如上url表示为:请求位于服务器localhost:8080上的SpringMVC站点的hello控制器。

    2、HandlerMapping为处理器映射。DispatcherServlet调用HandlerMapping,HandlerMapping根据请求url查找Handler。

    3、HandlerExecution表示具体的Handler,其主要作用是根据url查找控制器,如上url被查找控制器为:hello。

    4、HandlerExecution将解析后的信息传递给DispatcherServlet,如解析控制器映射等。

    5、HandlerAdapter表示处理器适配器,其按照特定的规则去执行Handler。

    6、Handler让具体的Controller执行。

    7、Controller将具体的执行信息返回给HandlerAdapter,如ModelAndView。

    8、HandlerAdapter将视图逻辑名或模型传递给DispatcherServlet。

    9、DispatcherServlet调用视图解析器(ViewResolver)来解析HandlerAdapter传递的逻辑视图名。

    10、视图解析器将解析的逻辑视图名传给DispatcherServlet。

    11、DispatcherServlet根据视图解析器解析的视图结果,调用具体的视图。

    12.最终视图呈现给用户。


    hello,SpringMVC 

    1.导包

    配置tomcat

      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
        <!-- Spring MVC 及 Spring系列包 -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>4.3.24.RELEASE</version>
        </dependency>
        <!--Servlet核心-->
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>4.0.1</version>
        </dependency>
        <!-- JSTL -->
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
        </dependency>
      </dependencies>

    注意资源导出问题:

       <!--解决资源导出问题-->
        <resources>
          <resource>
            <directory>src/main/java</directory>
            <includes>
              <include>**/*.properties</include>
              <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
          </resource>
          <resource>
            <directory>src/main/resources</directory>
            <includes>
              <include>**/*.properties</include>
              <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
          </resource>
        </resources>

    2.编写web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/javaee"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
             http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
             id="WebApp_ID" version="3.0">
       <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>
         <load-on-startup>1</load-on-startup>
       </servlet>
      
      <!--
    /和/*的区别:
    < url-pattern > / </ url-pattern > 不会匹配到.jsp, 只针对我们编写的请求;
      即:.jsp不会进入spring的 DispatcherServlet类 。
    < url-pattern > /* </ url-pattern > 会匹配*.jsp,
      会出现返回jsp视图时再次进入spring的DispatcherServlet 类,导致找不到对应的controller所以报404错。
    -->
    
      <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>

    注意点:

    servlet-mapping 中的url-pattern  

    /和/*的

    区别: / 只针对我们编写的请求,即.jsp不会进入spring的dispatcherservlet类中.

        /* 会匹配*.jsp会出现返回jsp视图时再次进入spring的dispatcherservlet类,导致找不到对应的controller所以报404的错误.

    3.编写SpringMVC的核心配置文件

    在springmvc的核心配置文件中只用编写4个部分:

    1.扫描指定包的注解

    2.配置资源过滤

    3.支持注解驱动

    4.配置视图解析器

    <?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:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           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/mvc
            https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <!--扫描指定包下的注解,让指定的类能够被IOC容器管理-->
        <context:component-scan base-package="org.west.controller"/>
    
        <!--静态资源过滤-->
        <mvc:default-servlet-handler/>
        <!--支持注解驱动-->
        <mvc:annotation-driven/>
    
        <!--配置视图解析器-->
    
        <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
        
    </beans>

    注意点:要导入对应的文件约束.

    4.编写Controller类

    package org.west.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    
    @Controller
    @RequestMapping("h1")
    public class HelloController {
    
        @RequestMapping("hello")
        public String hello(Model model){
    
             model.addAttribute("msg","hello,springMVC");
            return "hello";
        }
    
    }

    5.编写.jsp文件

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>SpringMVC</title>
    </head>
    <body>
    
    ${msg}
    
    </body>
    </html>

    6.运行结果

    
    
  • 相关阅读:
    数据结构-树与二叉树-思维导图
    The last packet successfully received from the server was 2,272 milliseconds ago. The last packet sent successfully to the server was 2,258 milliseconds ago.
    idea连接mysql报错Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone' property
    redis学习笔记
    AJAX校验注册用户名是否存在
    AJAX学习笔记
    JSON学习笔记
    JQuery基础知识学习笔记
    Filter、Listener学习笔记
    三层架构学习笔记
  • 原文地址:https://www.cnblogs.com/xiaoqiqistudy/p/11312118.html
Copyright © 2011-2022 走看看