Mainstream FRAMEWORK: SpringMVC (1) SpringMVC Overview

A: three-tier architecture and MVC

1, three-tier architecture:

Presentation Layer: that is, we often say that the web layer. It is responsible for receiving client requests, the response result to the client, the client usually using http protocol request web tier, web http request needs to receive, complete http response. Presentation layer comprising a display and control layers: the control layer is responsible for receiving the request, the results show demonstrate responsible layer. Presentation layer dependent on the service layer, the received client request layer will generally call the business service processing, and the processing result in response to the client. Design the presentation layer generally use MVC model. (MVC design model is the presentation layer, and other layers does not matter)

Business layer: that is, we often say that the service layer. It is responsible for business logic processing. layer web service layer dependent, but not dependent business layer web layer. Business layer in the business process may depend on the persistence layer, if you want to ensure data persistence required transactional consistency. (That is, we say, the transaction should be placed in the service layer to control)

Persistence: that is, we often say dao layer. Responsible for data persistence, including data layer, namely the database and the data access layer, the database is the data persistence carriers, data access layer is the interface service layer and persistence layer interaction, business layer needs through the data access layer will persist data to database. Popular speaking, persistence layer is to interact with the database, the database tables once excision investigation.

SpringMVC position in the three-tier architecture
Here Insert Picture Description

2、MVC

MVC full name is the Model View Controller, is a model (model) - view (view) - Controller (controller) the three most acronym, is a model for design to create a Web application presentation layer. MVC in each part of their duties:

Model:数据模型,JavaBean的类,用来进行数据封装

View:指JSP、HTML用来展示数据给用户。

Controller:用来接收用户的请求,整个流程的控制器。用来进行数据校验等。

3、SpringMVC概述

  1. 是一种基于Java实现的MVC设计模型的请求驱动类型的轻量级WEB框架
  2. Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供
    了构建 Web 应用程序的全功能 MVC 模块。
  3. 使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的
    SpringMVC框架或集成其他MVC开发框架
    ,如Struts1(现在一般不用),Struts2等。

二:SpringMVC的入门案例

SpringMVC是基于Spring在web层开发,所以需要创建web-app工程。

1、DispatcherServlet

DispatcherServlet:前端控制器,也是整个SpringMVC的核心控制器,由它调用其它组件处理用户的请求,dispatcherServlet的存在降低了组件之间的耦合性。

配置核心控制器 -一个Servlet

<!--  配置前端控制器-->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

2、创建SpringMVC配置文件

SpringMVC是基于Spring开发,所以在配置文件中,仍然与Spring一致,并且加入xmlns:mvc,同时使用注解开发方便,还需加入xmlns:context

xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"

同时与Spring一样要 开启注解的支持<mvc:annotation-driven注解要扫描的包<context:component-scan

在SpringMVC的各个组件中,处理器映射器、处理器适配器、视图解析器称为SpringMVC的三大组件。

使用mvc:annotation-driven自动加载RequestMappingHandlerMapping(处理映射器)和RequestMappingHandlerAdapter(处理适配器),可用在SpringMVC.xml配置文件中使用mvc:annotation-driven替代注解处理器和适配器的配置。

<!-- 开启springmvc框架注解的支持-->
<mvc:annotation-driven></mvc:annotation-driven>

<!-- 开启注解要扫描的包-->
<context:component-scan base-package="cn.itcast"></context:component-scan>

同时在核心控制器 DispatcherServlet 中加入 在应用加载时加载SpringMVC的配置文件contextConfigLocation

<!--  配置前端控制器-->
  <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:springmvc.xml</param-value>
    </init-param>

  <!-- 启动服务器就创建springmvc配置文件-->
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

3、配置视图解析器InternalResourceViewResolver

View Resolver负责将处理结果生成View视图(jsp),View Resolver首先根据逻辑视图名解析成物理视图名即具体的页面地址,再生成View视图对象,最后对View进行渲染将处理结果通过页面展示给用户。

<!-- 试图的解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 找什么目录下的什么类型的文件-->
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

4、创建控制器并使用注解配置

@Controller表示这是个控制器
@RequestMapping注解作用: 用于建立请求URL和处理请求方法之间的对应关系。

例如在类中加入/user,在方法中加入/hello,则当url为项目名称/user/hello时就加载控制器

/**
 * 控制器类
 * @author Mango
 */
@Controller
@RequestMapping(path = "/user")
public class HelloController {

    @RequestMapping(path = "/hello")
    public String sayHello() {
        System.out.println("hellc StringMVC");
       //默认表示jsp文件名字
        return "success";
    }

    /**
     * RequestMapping注解
     * @return
     */
    @RequestMapping(path = "/testRequestMapping")
    public String testRequsetMapping() {
        System.out.println("测试 RequestMapping注解...");
        return "success";
    }
}

5、RequestMapping注解

属性:

value / path:用于指定请求的URL。它和path属性的作用是一样的。

method:用于指定请求的方式。例如 method=RequestMethod.POST。就只能以post请求

params:用于指定限制请求参数的条件。它支持简单的表达式。要求请求参数的key和value必须和配置的一模一样。

例如: params = {“accountName”},表示请求参数必须有accountName

headers:用于指定限制请求消息头的条件。

6、入门案例的执行过程及原理分析

Here Insert Picture Description

1、案例执行过程

1、服务器启动,应用被加载。读取到web.xml中的配置创建spring容器并且初始化容器中的对象

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <!--  配置前端控制器-->
  <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:springmvc.xml</param-value>
    </init-param>

  <!-- 启动服务器就创建springmvc配置文件-->
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

2, the browser sends a request DispatherServlet is captured , the processing request is not Servlet, but forwards the request out. Forwarding path is in accordance with the request URL, the matching content @RequestMapping.

/**
 * 控制器类
 * @author Mango
 */
@Controller
@RequestMapping(path = "/user")
public class HelloController {

    @RequestMapping(path = "/hello")
    public String sayHello() {
        System.out.println("hellc StringMVC");

//        默认表示jsp文件名字
        return "success";
    }

    /**
     * RequestMapping注解
     * @return
     */
    @RequestMapping(path = "/testRequestMapping")
    public String testRequsetMapping() {
        System.out.println("测试 RequestMapping注解...");
        return "success";
    }
    
}

3, to match, the execution of the corresponding method. This method has a return value (e.g.: success).

4, according to the return value, means the InternalResourceViewResolver (trying parser) to find the corresponding result view .
Here Insert Picture Description
5, rendering results view , in response to the browser.

2, SpringMVC request response process

Here Insert Picture Description

Published 47 original articles · won praise 18 · views 4868

Guess you like

Origin blog.csdn.net/qq_43605085/article/details/100191114