[1] Spring MVC look ahead, review MVC

SpringMVC preview

ssm: myabtis+Spring+SpringMVC MVC three-tier architecture

JavaSE: study hard, lead by the teacher, get started quickly

JavaWeb: study hard, lead by teacher, get started quickly

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

SpringMVC + Vue + SpringBoot + SpringCloud + Linux

Spring:IOC和AOP

SpringMVC theory: SpringMVC execution process

SpringMVC operation: SSM framework integration

1. Review MVC

1.1, what is MVC

Model (dao, service), view (view) (jsp), controller (controller) (Servlet)

  • MVC is an abbreviation of Model, View, and Controller, and is a software design specification.
  • It is a method of separating business logic, data, and display to organize code.
  • The main function of MVC is to reduce the two-way coupling between view and business logic .
  • MVC is not a design pattern, MVC is an architectural pattern . Of course, there are differences between different MVCs.

**Model (model): **Data model, provides the data to be displayed, so it contains data and behavior, can be considered as a domain model or JavaBean components (including data and behavior), but now generally separated: Value Object ( Data Dao) and service layer (behavior Service). That is, the model provides functions such as model data query and model data status update, including data and business.

**View: **Responsible for the display of the model, which is generally the user interface we see and what customers want to see.

**Controller (controller): **Receive user requests, delegate to the model for processing (state change), and return the returned model data to the view after processing, and the view is responsible for display. In other words, the controller does the job of a dispatcher.

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

Insert picture description here

1.2, Model1 era

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

Insert picture description here

Model1 advantages: simple structure, more suitable for small project development;

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

1.3, Model 2 era

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

Insert picture description here

  1. User request
  2. Servlet receives request data and calls the corresponding business logic method
  3. After the business is processed, return the updated data to the servlet
  4. The servlet turns to JSP, and the page is rendered by JSP
  5. Respond to the updated page of the front end

Responsibility analysis:

Controller: Controller

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

Model: Model

  1. Business logic
  2. State of saved data

View: View

  1. Display page

Model2 not only improves code reuse rate and project scalability, but also greatly reduces project maintenance costs. The realization of Model 1 mode is relatively simple and suitable for rapid development of small-scale projects. The JSP page in Model1 has both the roles of View and Controller. The control logic and presentation logic are mixed together, resulting in very low code reusability and increased Application scalability and difficulty of maintenance. Model2 eliminates the shortcomings of Model1.

1.4, review Servlet

  1. Create a new Maven project as the parent project! pom dependent!

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.0</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. Create a Moudle: springmvc-01-servlet, add framework support, upgrade the project to a web project

  3. Import the jar dependencies of servlet and jsp in the pom.xml in the module

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

    package com.kuber.servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class HelloServlet extends HttpServlet {
          
          
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
          
          
            //1.获取前端参数
            String method = req.getParameter("method");
            if(method.equals("add")){
          
          
                req.getSession().setAttribute("msg","执行了add方法");
            }else if(method.equals("delete")){
          
          
                req.getSession().setAttribute("msg","执行了delete方法");
            }
            //2.调用业务层
    
            //3.视图转发或者重定向
            req.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(req,resp);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
          
          
            doGet(req, resp);
        }
    }
    
  5. Write hello.jsp, create a new jsp folder under the WEB-INF directory, create a new hello.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body >
        ${msg}
    </body>
    </html>
    
  6. Register servlet in web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
             version="5.0">
    
        <servlet>
            <servlet-name>hello</servlet-name>
            <servlet-class>com.kuber.servlet.HelloServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>hello</servlet-name>
            <url-pattern>/ddd</url-pattern>
        </servlet-mapping>
    </web-app>
    
  7. Configure tomcat and start the test

    Enter the url and assign the method:

    localhost:8080/ddd?method=add或者localhost:8080/ddd?method=delete

    Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43215322/article/details/110944046