Spring MVC principle and configuration

Spring MVC principle and configuration

1. Spring MVC overview

Spring MVC It is a powerful and flexible web framework provided by Spring. With the help of annotations, Spring MVC provides a development model that is almost POJO, making the development and testing of the controller easier. These controllers generally do not directly process requests, but delegate them to other beans in the Spring context. These beans are injected into the controller through Spring’s dependency injection function.

Spring MVC is mainly composed of DispatcherServlet, processor mapping, processor (controller), view resolver, and view.

2. SpringMVC operation principle

share picture

3.SpringMVC interface explanation

3.1 DispatcherServlet interface

Spring provided The front controller, all requests are distributed uniformly through it. Before DispatcherServlet distributes the request to Spring Controller, it needs to locate the specific Controller with the help of HandlerMapping provided by Spring.

3.2 HandlerMapping interface

It can complete the mapping of client requests to Controller.

3.3 Controller interface

The above request needs to be processed for concurrent users. Therefore, when implementing the Controller interface, it must be thread-safe and reusable.
Controller will process user requests, which is consistent with the role played by Struts Action. Once the Controller has processed the user request, it returns the ModelAndView object to the DispatcherServlet front controller, and ModelAndView contains the model (Model) and the view (View).
From a macro perspective, DispatcherServlet is the controller of the entire Web application; from a micro perspective, Controller is the controller in the processing of a single Http request, and ModelAndView is the model and view (View) returned during the Http request. ).

3.4 ViewResolver interface

The view resolver (ViewResolver) provided by Spring finds the View object in the web application and renders the corresponding result to the client.

4. DispatcherServlet

is the core of the entire Spring MVC. It is responsible for receiving HTTP requests, organizing and coordinating the various components of Spring MVC. Its main tasks are as follows:

  1. Intercept URL requests that conform to a specific format.
  2. Initialize the DispatcherServlet context corresponding to the WebApplicationContext, and associate it with the WebApplicationContext of the business layer and the persistence layer.
  3. Initialize the components of Spring MVC and assemble them into DispatcherServlet.

5. Example

5.1 Configure web.xml file


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

springmvc
org.springframework.web.servlet.DispatcherServlet


contextConfigLocation< br /> classpath:springmvc.xml

1< br />



springmvc
/




CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter

encoding
utf-8



CharacterEncodingFilter
/*

5.2 Configure springmvc.xml file


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/mvc http ://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans /spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">







< /property>



As the above two main configuration files are successfully configured, the following code is written

5.3 main code preparation

  1. reg.jsp

    <%@ page contentType="text/html;charset=UTF- 8" language="java" %>


    User registration




    Create user


    < label>Name:















  2. success.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    < html>

    Title



    Registered successfully



    The user information is as follows:


    h5>
    Name: ${user.name}
    Age: ${user.age}
    Password: ${user.pwd}




  3. UserController.java

    package mrzhangxd .xyz.controller;

    import mrzhangxd.xyz.pojo.User;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;

    @Controller
    public class UserController {
    @RequestMapping("")
    public String Create(Model model) {
    return "reg/reg";
    }
    @RequestMapping("/save")
    public String Save(@ModelAttribute("form") User user, Model model) {
    model.addAttribute("user", user);
    return "reg/success";
    }
    }
  4. User.java

    package mrzhangxd.xyz.pojo;

    import java.io.Serializable;
    import java.util.Date;

    public class User implements Serializable {< br />
    private static final long serialVersionUID = 1L;
    private Integer id; //User ID
    private String name; // Username
    private String pwd; // Password
    private Integer age; // age
    private Date creatTime; // creation time

    public Integer getId() {
    return id;
    }< br />
    public void setId(Integer id) {
    this.id = id;
    }

    public String getName() {
    return name ;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getPwd() {
    return pwd;
    }

    public void setPwd(String pwd) {
    this.pwd = pwd;
    }

    public Integer getAge() {
    return age;
    }

    public void setAge(Integer age) {
    this.age = age;
    }

    public Date getCreatTime() {
    return creatTime;
    }

    public void setCreatTime(Date creatTime) {
    this.creatTime = creatTime;
    }
    }

5.4 Run results

  1. Registration page
    Share pictures

  2. Success page
    Share a picture

— END —

Leave a Comment

Your email address will not be published.