Spring mvc-part01 what is spring MVC

Keywords: Java Spring mvc

1,SpringMVC

SSM: mybatis + Spring + spring MVC MVC three-tier architecture

JavaSE: study hard, teachers lead, and get started quickly

JavaWeb: study hard, teachers lead, and get started quickly

SSM framework: study official documents, exercise self-study ability, exercise note taking ability and exercise project ability

SpringMVC+Vue+SpringBoot+SpringCloud+Linux

SSM=JavaWeb project

Spring: IOC and AOP

Spring MVC: the execution process of spring MVC!

Spring MVC: SSM framework integration!

Spring

1.1 what is MVC?

  • MVC: model (dao,service) view (jsp) controller (Servlet), which is a software design specification
  • It is a method to organize code by separating business logic, data and display
    • dao, service, servlet: forwarding, redirection, jsp/html
  • The main function of MVC is to reduce the two-way coupling between view layer and business logic
  • MVC is not a design pattern, MVC is an architecture pattern. Of course, different MVCs are different

Model: the data model provides the data to be displayed, so it contains data and behavior. It can be considered as a domain model or JavaBean component (including data and behavior), but now it is separated like a card: value object (data Dao) and service layer (behavior Service) In other words, the model provides functions such as model data query and model data status update, including data and business.

View: it is responsible for displaying the model, which is generally the user interface we see and what customers want to see.

**Controller: * * receives the user's request and delegates it to the model for processing (state change). After processing, the returned model data is returned to the view, which is responsible for displaying. That is, the controller does the work of a dispatcher.

The most typical MVC is the pattern of JSP + servlet + javabean.

Front end data transmission entity class

Entity class: user name, password... 20

Front end: user name, password

pojo: User

vo:UserVo

dto:

JSP: essentially a servlet

1.2. Model1 Era

  • In the early development of web, model 1 is usually used.
  • Model1 is mainly divided into two layers: view layer and model layer.

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-fx3vdmmo-163869167066) (E: \ desktop \ document \ javaweb\SpringMVC.assets\image-20211205175514321.png)]

Advantages of Model1: simple architecture, more suitable for small project development;

Model1 disadvantages: JSP responsibilities are not single, responsibilities are too heavy, and it is not easy to maintain;

1.3. Model 2 era

Model 2 divides a project into three parts, including view, control and model.

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-m7boc6c3-163869167069) (E: \ desktop \ document \ javaweb\SpringMVC.assets\image-20211205175538023.png)]

  1. User sends request
  2. The Servlet receives the request data and calls the corresponding business logic method
  3. After the business is processed, the updated data is returned to the servlet
  4. servlet turns to JSP, which renders the page
  5. Respond to the front-end updated page

Responsibility analysis:

Controller: controller

  1. Get form data
  2. Call business logic
  3. Go to the specified page

Model: Model

  1. Business logic
  2. Status of saved data

View: View

  1. Display page

In this way, Model2 not only improves the reuse rate of code and the scalability of the project, but also greatly reduces the maintenance cost of the project. The implementation of Model 1 mode is relatively simple and suitable for rapid development of small-scale projects. The JSP page in Model1 plays both the roles of View and Controller, mixing the control logic and presentation logic together, resulting in very low code reuse and increased application Model 2 eliminates the disadvantages of Model 1.

1.4. Review Servlet

  1. Create a Maven project as the parent project! pom dependency!

    <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>
    
  2. Establish a Moudle: springmvc-01-servlet and add Web app support!

  1. Import jar dependencies for servlet s and JSPS

    <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>
    
  2. Write a Servlet class to handle user requests

    package com.kuang.servlet;
    
    //Implement Servlet interface
    public class HelloServlet extends HttpServlet {
       @Override
       protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
           //Get parameters
           String method = req.getParameter("method");
           if (method.equals("add")){
               req.getSession().setAttribute("msg","Yes add method");
          }
           if (method.equals("delete")){
               req.getSession().setAttribute("msg","Yes delete method");
          }
           //Business logic
           //View jump
           req.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(req,resp);
      }
    
       @Override
       protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
           doGet(req,resp);
      }
    }
    
  3. Write hello.jsp, create a JSP folder under the WEB-INF directory, and create hello.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
       <title>Kuangshen</title>
    </head>
    <body>
    ${msg}
    </body>
    </html>
    
  4. Registering servlets in web.xml

    <?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>HelloServlet</servlet-name>
           <servlet-class>com.kuang.servlet.HelloServlet</servlet-class>
       </servlet>
       <servlet-mapping>
           <servlet-name>HelloServlet</servlet-name>
           <url-pattern>/user</url-pattern>
       </servlet-mapping>
    
    </web-app>
    
  5. Configure Tomcat and start the test

    • localhost:8080/user?method=add

    • localhost:8080/user?method=delete

What does the MVC framework do

  1. Mapping URLs to java classes or methods of java classes
  2. Encapsulates data submitted by users
  3. Processing request - calling related business processing - encapsulating response data
  4. Render the response data. jsp / html and other presentation layer data

explain:

Common server-side MVC frameworks include Struts, Spring MVC, ASP.NET MVC, Zend Framework and JSF; common front-end MVC frameworks include vue, angularjs, react and backbone; other models have evolved from MVC, such as MVP, MVVM and so on

Posted by gazolinia on Sun, 05 Dec 2021 04:44:30 -0800