Introduction to Spring MVC - Introduction, Workflow, Adapter Mode

Posted by boske on Sun, 04 Aug 2019 15:33:31 +0200

Catalog

  1. Introduction to spring MVC
  2. Spring MVC workflow
  3. Spring MVC adapter mode

Introduction to Spring MVC

Spring MVC is a lightweight Web framework provided by Spring that implements the design pattern of Web MVC. Like Struts 2 framework, they belong to MVC framework, but their usage and performance are better than Struts 2.
Characteristic:
1) It is a part of Spring framework, which can make use of other functions provided by Spring conveniently.
2) It is flexible and easy to integrate with other frameworks.
3) Provide a front-end controller Dispatcher Servlet, so that developers do not need to develop additional controller objects;
4) It can automatically bind user input and convert data types correctly.
5) Built-in common checkers, which can teach and research user input. If the check fails, it will be redirected to the input form.
6) Support internationalization. Avoidance language can be displayed according to user area.
7) Support multiple template engines: JSP, beetl, FreeMarker, Velocity, etc.
8) Using XML-based configuration files, after editing, there is no need to recompile the application.

2. Spring MVC workflow:


(1) When a user sends a request to the server through a browser, the request will be intercepted by the front-end controller Dispatcher Servlet of Spring MVC.

After the Dispatcher Servlet intercepts the request, it calls the Handler Mapping processor mapper.

(3) The processor mapper finds the specific processor according to the request URL, generates the processor object and the processor interceptor (if any) and returns them to Dispatcher Servlet.

(4) Dispatcher Servlet will select the appropriate handler adapter (processor adapter) by returning information;

Handler Adapter calls and executes Handler, where the processor is worth the Controller class written in the program, also known as the back-end controller.

After the Controller is executed, a Model AndView object will be returned, which will contain the view name or the model and view name.

Handler Adapter returns the ModelAndView object to Dispatcher Servlet;

Dispatcher Servlet will select an appropriate ViewReslover (View Parser) according to the Model AndView object;

_View Reslover parses and returns a specific view to the Dispatcher Servlet.

Dispatcher Servlet renders View (filling model data into view); the result of view rendering is returned to client browser for display.

Spring Core Package and Spring MVC Core Package:

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>5.1.6.RELEASE</version>
		</dependency>
		
		<!--  Spring MVC package -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>

3. Spring MVC - Adapter Mode

Benefits:

If new components or standards are added after the code is published, users can write according to their needs. Just add a component and its corresponding adapter. The standard is in the adapter adapter adapter, which decouples the standards and implementations. The user writes a class, plus the adapter he needs, tells Tomcat how to run and writes the handle himself, so he doesn't have to follow the http component standard. Just follow your own adapter standards. It's up to you to handle whatever you want to do first and then what you want to do next.

Firstly, the summary structure of adapter pattern is realized by using HttpHandlerAdapter, SimpleHandler Adapter and Annotation Handler Adapter respectively.


1) Create interfaces for components and adapters

public interface MockController {
	
}

/*
 * Spring MVC The adapter mode of
 */
public interface MockAdapter {
	
	//Pass in a class to determine if the current class can be adapted by the adapter
	public boolean support(Object handler);
	
	//If the current adapter can be adapted, this method is called to process, handle processing, and the object handler is currently processing.
	public void handle(Object handler);
}

2) Controller components of three major implementations:

/*
 * Spring MVC Controller component implemented through bean name
 * 
 * To make components work, you must add adapters that make them work: HttpH and leAdapter, or http processing adapter.
 */
public class HttpController implements MockController{

	public void doHttpController() {
		// TODO Auto-generated method stub
		System.out.println("HttpController");
	}
}


/*
 * Spring MVC The adapter mode is implemented by simple mapping
 */
public class SimpleController implements MockController{
	public void doSimpleController() {
		System.out.println("SimpleController");
	}
}


/*
 * Spring MVC The adapter mode is implemented by annotation
 */
public class AnnotationController implements MockController{

	public void doAnnotationController() {
		System.out.println("AnnotationController");
	}
}

3) Write three kinds of adapters and implement their standards

support method: Pass in a class object to determine whether the current class can be adapted by the adapter

Handle method: If the current adapter can be adapted, this method is called for processing, handle processing, and the object handler is currently processing.

HttpHandleAdapter .java

/*
 * Implementing adapter interfaces, adapters and controllers are provided by users
 */
public class HttpHandleAdapter implements MockAdapter{
	
	//handler handling bean name
	public boolean support(Object handler) {
		
		//If the incoming class is a subclass of HttpController
		if(handler instanceof HttpController) {
			return true;
		}
		return false;
	}

	//Logical confirmation by support method is a subclass of HttpController, so it can be strongly transformed directly.
	public void handle(Object handler) {
	
		HttpController httpController = (HttpController) handler;
		httpController.doHttpController();
	}
}

SimpleHandlerAdapter.java and AnnotationHandlerAdapter.java implementations are basically the same as above, skipping...

4) Dispatcher Servlet front-end controller simulating Spring MVC framework:

/*
 * Framework source code, simulation of spring MVC Dispatcher Servlet, its core logic out
 */

import java.util.ArrayList;
import java.util.List;

public class MockDispatcherServlet {
	
	//All adapter implementations are collected in the collection, and spring collects all existing adapters and automatically injects them through @Autowire
	//But there's no spring at this point, so rewrite the constructor and create all adapter s
	private List<MockAdapter> adapters= new ArrayList<MockAdapter>();
	
	//Construction method
	public MockDispatcherServlet() {
		adapters.add(new HttpHandleAdapter());
		adapters.add(new SimpleHandleAdapter());
		adapters.add(new AnnotationHandleAdapter());
	}
	
	public static void main(String[] args) {
		new MockDispatcherServlet().doService();
	}

	public void doService() {
		AnnotationController annoController = new AnnotationController();
		//HttpController httpHontroller = new HttpController();
		//SimpleController simpleHontroller = new SimpleController();

		//Traverse through all adapters
		for (MockAdapter mockAdapter : adapters) {
			//If the current user requests support
			if (mockAdapter.support(annoController)) {
				//Execution of appropriate methods
				mockAdapter.handle(annoController);//AnnotationController
			}
		}
	}
}

Topics: Spring Java Struts JSP