1. Review for SpringMVC MVC
One. What is MVC
– MCV is a software design specification short for Model, View, and Controller.
– Is a way to organize code by separating business logic, data, and display.
– MVC reduces the bidirectional coupling between views and services.
– MVC is not a design pattern. MVC is an architectural pattern. There are differences between MVC’s.
Model: Data Model, which provides the data to be presented and therefore contains the data and behavior, can be thought of as a domain Model or JavaBean component (containing the data and behavior), but is now generally separated: Value Object (Data Dao) and behavior Service layer (behavior Service). That is, the model provides model data query and model data status update functions, including data and business.
View: Is responsible for presenting the model, which is generally the user interface we see, what the customer wants to see.
Controller (Controller) : Receives user requests, delegates them to the model for processing (state changes), and returns the returned model data to the view, which is responsible for displaying it. So the controller is doing the job of a dispatcher.
The most classic MVC is JSP+Servlet+ Javabean
Review the Servlet
1. The process
-
Create a new regular Maven parent project, SpringMVCCode
-
Import the JAR package in the parent project
-
Create a new normal Maven subproject, SpringMVC-01-Servlet
-
Import the JAR package in the subproject
-
Subproject adds framework support, Web Application
-
Create a new com/lyw/servlet directory in the Java directory and add helloservlet.java
-
Add the JSP folder in web-INF and add Hello.jsp
-
Register in web.xml
-
Add the Tomcat server. The package path is empty
-
Run the test
Import the JAR package in the parent project
<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>
Copy the code
Create a new generic Maven subproject, SpringMVC-01-Servlet
Import the JAR package in the subproject
<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>
Copy the code
Subproject adds framework support, Web Application
Create a new com/lyw/servlet directory in the Java directory and add helloservlet.java
// Implement the 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"."Execute add method");
}
if (method.equals("delete")){
req.getSession().setAttribute("msg"."Delete method executed");
}
// 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); }}Copy the code
Add the JSP folder in web-INF and add Hello.jsp
<%@ page contentType="text/html; charset=UTF-8" language="java" %><html>
<head>
<title>Kuangshen</title>
</head>
<body>
${msg}
</body>
</html>
Copy the code
Register in web.xml
<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>
Copy the code
Add the Tomcat server and set the packaging path to empty
Run the test
Enter the path
Localhost: 8080 / user? method=add
Localhost: 8080 / user? method=delete
Data is obtained by request forwarding
conclusion
What does an MVC framework do
- A method that maps a URL to a Java class or Java class.
- Encapsulate user-submitted data.
- Process the request — invoke the relevant business process — encapsulate the response data.
- Render the response data. JSP/HTML and other surface data