Preface:

It is better to write a learning record, especially for some operation parts. Some questions about the concept refer to the notes of the crazy god. Video courses are also available at station B. In addition, this tutorial has been around for two or three years, and it is not necessarily the most novel technology in the current actual development (see the bullet screen, some people make fun of it, prevent 0.0 in advance). It is just a record of learning from xiaomaixiang. if there is any mistake, please XDM correction.

Ps: Official documentation for SpringMVC

1. Review MVC

1.1. What is MVC

1.2 Model1 era

1.3 Model2 era

1.4. Review servlets

1. Create maven project (common, do not check) after the parent project is created, delete the default generated SRC, and then import dependencies (try not to directly copy, manually copy, it may not be successful).

<! --> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> < version > 5.1.9. RELEASE < / version > < / dependency > < the dependency > < groupId > javax.mail. Servlet < / groupId > < < artifactId > servlet - API/artifactId > < version > 2.5 < / version > < / dependency > < the 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>Copy the code

You can see it working on the next package here

2. Create a Moudle: SpringMVC-01 and add Web App support.

Add a generic item and don’t select anything. Then add Web support as shown below

  1. Import jar dependencies for servlets and JSPS.

4. Write a Servlet class to handle user requests. (See the different handling methods in this step, after I learn how to write Hello World, I will explore them.)

// Add the class to the package. // Add the class to the package.

This is just a normal class that needs to inherit HttpServlet

Override the father’s two methods, doGet and doPost

public class hello 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 "," execute add method "); } if (method.equals("delete")){req.getSession().setattribute (" MSG "," delete"); // Create a new directory under web/WEB-INF named JSP // create a new page named test.jsp // in the body tag. ${MSG} return MSG req.getrequestDispatcher ("/ web-inf/JSP /test.jsp"). Forward (req,resp); Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); //doPost call doGet}}Copy the code

5. Configure the servlet in web/WEB_INF/web.xml

6. Another page is needed at this point

7. Configure Tomcat as shown in the video. It will work

// If tomcat is not available, see: Install Tomcat

9. The startup is successful

localhost:8080/hello? method=add

localhost:8080/hello? method=delete

What does an MVC framework do

  1. A method that maps a URL to a Java class or Java class.
  2. Encapsulate user-submitted data.
  3. Process the request — invoke the relevant business process — encapsulate the response data.
  4. Render the response data. JSP/HTML and other presentation layer data

Common server-side MVC frameworks are: Struts, Spring MVC, ASP.NET MVC, Zend Framework, JSF; Common front-end MVC frameworks: Vue, AngularJS, React, backbone; Other patterns have evolved from MVC such as MVP, MVVM, and so on….

2. What is SpringMVC

2.1 overview,

Spring MVC, part of the Spring Framework, is a lightweight Web Framework that implements MVC based on Java.

2.2. Central controller

2.3 Implementation principle of SpringMVC