What is MVC

MVC is actually an architectural idea that divides software into models, views, and controllers.

  • M: Model is a JavaBean used to process data.
  • V: View layer refers to the HTML or JSP page in the project, which is used to interact with users and display data.
  • C: The Controller is a servlet that receives requests and responds to the browser.

There are two types of Javabeans in M:

  • Entity class beans: those that specifically store business data, such as Student, User, etc.
  • Business process Bean: A Service or Dao object that is specifically used to handle business logic and data access.

MVC workflow

  1. The user sends the request to the server through the view layer
  2. The request is received by the Controller in the server
  3. The Controller calls the appropriate Model layer to process the request
  4. The result is returned to the Controller
  5. The Controller then finds the corresponding View according to the result of the request processing, renders the data and finally responds to the browser

What is SpringMVC

SpringMVC is a follow-up product of Spring and a sub-project of Spring. SpringMVC is a complete set of solutions provided by Spring for presentation layer development.

At present, the industry generally chooses SpringMVC as the first choice for the development of Java EE project presentation layer.

The characteristics of for SpringMVC

  • The Spring family of native products seamlessly connects to infrastructure such as IOC containers
  • Based on native servlets, through the powerful front-end controller DispatcherServlet, requests and responses for unified processing
  • The presentation layer covers the problems that need to be solved in all segments and provides comprehensive solutions
  • Code fresh and concise, greatly improve the development efficiency
  • High degree of internal componentizing, pluggable components plug and play, what functions you want to configure the corresponding components
  • Outstanding performance, especially suitable for modern large, super – large Internet project requirements

Iii. Development environment preparation

The development environment needs to be ready before you can implement a world-renowned program.

  • IDE: 2018.3 the idea
  • Build tool: Maven3.5.4
  • Server: Tomcat8
  • Spring version: 5.3.1

4. Implement Hello World

Move your hands together.

1. Create a project

I’m going to create a new project, and I’m going to call it SpringMVC. Then create a new module in it, called SpringmVC-Demo1, and finally complete.

Note that the web module is added in the file-project Structure.

2. Introduce dependencies

In the pom.xml file under the module SpringmVC-Demo1, the dependencies need to be introduced.

<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < the parent > < artifactId > for springmvc < / artifactId > < groupId > com, pingguo. MVC < / groupId > < version > 1.0 - the SNAPSHOT < / version > < / parent > The < modelVersion > 4.0.0 < / modelVersion > < artifactId > for springmvc - not < / artifactId > <! --> <packaging>war</packaging> <dependencies> <! -- SpringMVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> The < version > 5.3.1 < / version > < / dependency > <! <dependency> <groupId> ch.qs.logback </groupId> <artifactId>logback-classic</artifactId> The < version > 1.2.3 < / version > < / dependency > <! -- ServletAPI --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> The < version > 3.1.0 < / version > < scope > provided < / scope > < / dependency > <! <groupId>org.thymeleaf</groupId> <artifactId> Thymeleaf </artifactId> </artifactId> < version > 3.0.12. RELEASE < / version > < / dependency > < / dependencies > < project >Copy the code

3. Configure web. XML

In the webapp web. XML configuration:

<? The 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" > <! -- Configure SpringMVC's front-end controller, --> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <! -- Specify the location and name of the SpringMVC configuration file with initialization parameters --> <init-param> <! -- contextConfigLocation = contextConfigLocation --> <param-name>contextConfigLocation</param-name> <! -- Use classpath: to find the configuration file from the classpath, SRC /main/resources --> <param-value>classpath: springmVc.xml </param-value> </init-param> <! -- As the core component of the framework, There is a lot of initialization that needs to be done during startup and this is a serious impact on access speed when the DispatcherServlet is first requested so you need to use this tag to pre-initialize the startup control DispatcherServlet to server startup --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <! -- Set the request path that springMVC's core controller can handle. The request path can be /login or.html or.js or.css but/does not match the.jsp request path --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>Copy the code

3. Configure the Spring configuration file

Create a new profile under Resources.

<? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="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.xsd"> <! - automatic scanning package - > < context: component - scan base - package = "com. Pingguo. MVC. Controller" > < / context: component - scan > <! - configuration parser Thymeleaf view - > < bean id = "viewResolver" class = "org. Thymeleaf. Spring5. The ThymeleafViewResolver" > < property name="order" value="1"/> <property name="characterEncoding" value="UTF-8"/> <property name="templateEngine"> <bean class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver"> <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <! Templates ="/ web-INF /templates/"/> <! -- View suffix --> <property name="suffix" value=".html"/> <property name="templateMode" value="HTML5"/> <property name="characterEncoding" value="UTF-8" /> </bean> </property> </bean> </property> </bean> </beans>Copy the code

Here, the view layer uses Thymeleaf, so don’t worry about separating the front end and the back end, you’ll learn it step by step.

4. Write the request controller

Because the front controller handles requests sent by the browser uniformly, but specific requests have different processing processes, you need to create a class that handles specific requests, namely the request controller.

Each method in the request controller that handles the request becomes a controller method.

Since SpringMVC’s Controller is held by a POJO (plain Java class), it needs to be identified as a control layer component through the @Controller annotation and handed over to Spring’s IoC container to recognize the existence of the Controller.

package com.pingguo.mvc.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @controller public class HelloController {// @requestMapping Handle the mapping between the request and controller methods // the @requestMapping annotation's value property matches the request by the request address, @requestMapping (value = "/") public String index() {// Return view name "index"; } @RequestMapping("/target") public String toTarget() { return "target"; }}Copy the code

Here I just added two methods, two pages.

5. Write a page file

Under webapp/ web-INF /templates, write the HTML file.

index.html

<! DOCTYPE HTML > < HTML lang="en" XMLNS :th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> The < / head > < body > < h1 > Hello World < / h1 > < a th: href = "@ {/ target}" > access to the target. The target page HTML < / a > < / body > < / HTML >Copy the code

A tag is added here to jump to another page, target.html. Href =”@{/target} “href=”@{/target}” Because you’re going to end up developing applications in a way that separates the front end and the back end.

target.html

<! DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>target</title> </head> <body> <h1>target</h1> </body> </html>Copy the code

6. Start the project

Because the project is packaged as a WAR package and deployed in Tomcat, you need to install tomcat locally first, and there are a lot of tutorials online.

Then, add Run Configuration in idea.

The first Deployment.

Here the Application of the context is the Application context, such as I want to visit/target, actual project started in the browser to access is: http://localhost:8080/springmvc/target.

The next step is to configure the Server.

Start, can run or debug, debug can be used to debug breakpoints.

After launch, the default will open at http://localhost:8080/springmvc/.

Click the jump link on the Index page to successfully jump to the Target page.

To review the process of requesting a page:

  • The browser sends a request, and if the request address matches the url-pattern of the front controller, the request is processed by the front controller DispatcherServlet.
  • The front-end controller reads the SpringMVC core configuration file, scans the components to find the controller, and matches the request address with the value property in the @RequestMapping annotation on the controller. If the match is successful, the controller method identified by the annotation is the method that processes the request.
  • The method that processes the request needs to return a string view name, which is resolved by the view parser, prefixed and suffixed to form the path of the view, rendered through Thymeleaf, and forwarded to the corresponding page of the view.

Thanks for the learning resource of “Shangsilicon Valley”.