SpringMVC works Introduction

First, understand what is the MVC design pattern

  • Cotroller: responsible for receiving and processing the request, the response client
  • Model: model data, business logic
  • View: rendering model, interaction with the user

 MVC implementation process:

        The user initiates a request, the control layer is received, then the call control layer to layer different models depending on the method of the request. Different business methods, have a persistent database CRUD operations, to produce a final model data. Then encapsulated data model, is passed to the view layer. View layer to get data is rendered, the final result of the response to the client, the user can see a final outcome. Next, the user performs the next operation, and a new request is made, then repeat the previous process. This is the MVC process.

Spring MVC What is it? 

  • The best framework for the realization of the MVC design pattern. MVC design pattern will have the whole package, will be shielded many of the underlying code, so developers can be more convenient and efficient to carry out application development.
  • SpringMVC is a successor to the Spring Framework.
  • Using spring characteristic of the container, simplify their configuration, corresponding to a sub-frame of the spring module, both well used in combination, it does not require integration.

 

SpringMVC core components 

  • DispatcherServlet: Front Controller

       The core of the flow control, for controlling the execution of other components, unified scheduling, reduce the coupling between the various components of
the position corresponding to commander

  • Handler: a processor to accomplish specific business logic

        DispatcherServlet when receiving the request, the request need to be different to different distributed among Handler

  • HandlerMapping: mapping request to the Handler
  • HandlerInterceptor: Processor interceptors

      HandlerInterceptor is an interface, complete the required intercept processing functions

  • HandlerExceptionChain: a processor to perform chain
  •  HandlerAdapter: Processor Adapter

      Handler before performing business methods require a series of operations, including verification of the form data, the form data type conversion and data encapsulated in the form java bean among the series of operations is completed by HandlerAdapter

  • MoudelAndView: package model and view information data

     View information refers to a logical view of, as a processing result Handler returns to DispatcherServlet

  • ViewResolver: View resolvers    

     After DispatcherServlet get MoudelAndView, need to be resolved, ViewResolver resolve the logical view of the physical view, the final result will be rendered to the client

 SpringMVC implementation process:

1, the client requests are DispatcherServlet received
2, DispatcherServlet maps the request to Handler
. 3, generates Handler and HandlerInterceptor from
. 4, return HandlerExceptionChain (Handler + HandlerInterceptor from)
. 5, DispatcherServlet by HandlerAdapter performing Handler
. 6, returns a MoudelAndView (model view of logical data) to the DispatcherServlet
. 7, on the DispatcherServlet ViewResolver MoudelAndView by parsing the logical view resolved into a physical view
8, returned filled view model data in response to the client for rendering.

 SpringMVC use:

1, most of the components provided by the framework, the developer only needs to be associated by the configuration
2, the developer only needs to manually write Handler, View

Based on the use annotation mode (recommended)

1、SpringMVC基础配置
2、Controller,HandlerMapping通过注解进行映射
3、XML配置ViewResolver组件映射

创建AnnotationHandler

package com.lzy.handler;

import com.lzy.entity.Goods;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

@Controller
public class AnnotationHandler {

    /**
     * 业务方法:ModelAndView完成数据的传递,视图的解析
     */
    @RequestMapping("/modelAndViewTest")
    public ModelAndView modelAndViewTest(){
        //创建ModelAndView对象
        ModelAndView modelAndView =  new ModelAndView();
        //填充模型数据
        modelAndView.addObject("name","Tom");
        //设计逻辑视图
        modelAndView.setViewName("show");
        return modelAndView;
    }

    /**
     * 业务方法:Model传值,String进行视图渲染解析
     */
    @RequestMapping("/ModelTest")
    public String ModelTest(Model model){
        //填充模型数据
        model.addAttribute("name","Jerry");
        //设置逻辑视图
        return "show";
    }

    /**
     * 业务方法:Map传值,String进行视图解析
     */
    @RequestMapping("MapTest")
    public String MapTest(Map<String,String>map){
        //填充模型数据
        map.put("name","Cat");
        //设计逻辑视图
        return "show";
    }

}

配置springmvc.xml

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

 <!--将AnnotationHandler自动扫描到IOC容器中-->
    <context:component-scan base-package="com.lzy.handler"></context:component-scan>

    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--配置前缀-->
        <property name="prefix" value="/"></property>
        <!--配置后缀-->
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

show.jsp

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

 

基于XML配置的使用

1、SpringMVC基础配置
2、XML配置Controller,HandlerMapping组件映射
3、XML配置ViewResolver组件映射

创建MyHandler

package com.lzy.handler;

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

public class MyHandler implements Controller {
    @Override
    public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception {
        //装载模型数据和逻辑视图
        ModelAndView modelAndView = new ModelAndView();
        //添加模型数据
        modelAndView.addObject("name","Tom");
        //添加逻辑视图
        modelAndView.setViewName("show");
        //   /show.jsp
        return modelAndView;
    }
}

 配置springmvc.xml

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

    <!--配置HandlerMapping,将url请求映射到Handler-->
    <bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

        <!--配置mapping-->
        <property name="mappings">
            <props>
                <!--配置test请求对应的handler-->
                <prop key="/test">testHandler</prop>
            </props>
        </property>
    </bean>

    <!--配置Handler-->
    <bean id="testHandler" class="com.lzy.handler.MyHandler"></bean>
<!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--配置前缀-->
        <property name="prefix" value="/"></property>
        <!--配置后缀-->
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

show.jsp

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

 

Guess you like

Origin blog.csdn.net/qq_41937388/article/details/90293163