Comprehensive analysis of advanced core knowledge in Java, nginx architecture diagram

<servlet-name>springmvc</servlet-name> 
<servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class> 
<!-- If not set init-param Label, you must/WEB-INF/Create under xxx-servlet.xml File, where xxx yes servlet-name The name of the configuration in. --> 
<init-param> 
	<param-name>contextConfigLocation</param-name> 
	<param-value>classpath:spring/springmvc-servlet.xml</param-value> 
</init-param> 
<load-on-startup>1</load-on-startup> 
SpringMVC / ` ` # IV. working principle of SpringMVC (important) * * simply put: * * client sends request - > front-end controller 'DispatcherServlet' accepts client request - > finds processor mapping 'HandlerMapping' resolves the Handler corresponding to the request - > 'handleradapter' will call the real processor to start processing the request according to the 'Handler', And process the corresponding business logic - > the processor returns a model view 'modelandview' > > the view parser parses it - > returns a view object - > the front-end controller 'dispatcher servlet' renders data (Moder) - > returns the obtained view object to the user

As shown in the figure below:

A clerical error in the above figure: the entry function of Spring MVC, that is, the DispatcherServlet of the front-end controller, is used to receive requests and respond to results.

Process description (important):

  1. The client (browser) sends a request directly to the dispatcher servlet.
  2. DispatcherServlet calls HandlerMapping according to the request information to parse the Handler corresponding to the request.
  3. After resolving to the corresponding Handler (that is, the Controller controller we usually call), it is processed by the HandlerAdapter adapter.
  4. The HandlerAdapter will call the real processor to process the request according to the Handler and process the corresponding business logic.
  5. After processing the business, the processor will return a ModelAndView object, k is the returned data object, and View is a logical View.
  6. ViewResolver will find the actual View based on the logical View.
  7. Dispatcher servlet passes the returned Model to View (View rendering).
  8. Return View to requester (browser)

5, Description of important spring MVC components

1) Front end controller DispatcherServlet (no engineer development required), provided by the framework (important)

Function: Spring MVC entry function. Receive request, response result, equivalent to repeater, CPU. The dispatcher servlet reduces the coupling between other components. When the user's request reaches the front-end controller, it is equivalent to c in mvc mode. Dispatcher servlet is the center of the whole process control. It calls other components to process the user's request. The existence of dispatcher servlet reduces the coupling between components.

2) The processor mapper handlermapping (not developed by engineers) is provided by the framework

Function: find the Handler according to the requested url. HandlerMapping is responsible for finding the Handler (Controller) according to the user's request. Spring MVC provides different mappers to implement different mapping methods, such as configuration file method, implementation interface method, annotation method, etc.

3) Processor adapter HandlerAdapter

Function: execute according to specific rules (rules required by the HandlerAdapter). The Handler executes the processor through the HandlerAdapter. This is an application of adapter mode. More types of processors can be executed through the extension adapter.

4) Processor Handler (to be developed by engineers)

Note: when writing a Handler, follow the requirements of the HandlerAdapter so that the adapter can execute the Handler correctly
Handler is the back-end controller following the front-end controller of dispatcher servlet. Under the control of dispatcher servlet, handler processes specific user requests.
Since the Handler involves specific user business requests, engineers generally need to develop the Handler according to business requirements.

5) The view resolver, view resolver (without Engineer Development), is provided by the framework

Function: analyze the view and convert it into a real view according to the logical view name
The View Resolver is responsible for generating the processing results into the View view. The View Resolver first resolves the logical View name into the physical View name, that is, the specific page address, and then generates the View object. Finally, it renders the View and displays the processing results to the user through the page.
Spring MVC framework provides many View types, including jstlView, freemarkerView, pdfView, etc.
Generally, the model data needs to be displayed to users through pages through page label or page template technology. Engineers need to develop specific pages according to business requirements.

6) View (to be developed by engineers)

View is an interface, and the implementation class supports different view types (jsp, freemaker, pdf...)

Note: the processor Handler (that is, the Controller we usually call) and the view layer view need to be developed manually by ourselves. Other components, such as front-end Controller DispatcherServlet, processor mapper HandlerMapping, processor adapter HandlerAdapter, etc., are provided to us by the framework and do not need to be developed manually.

6, Detailed analysis of dispatcher Servlet

First, look at the source code:

package org.springframework.web.servlet; 

@SuppressWarnings("serial") 
public class DispatcherServlet extends FrameworkServlet { 

	public static final String MULTIPART_RESOLVER_BEAN_NAME = "multipartResolver"; 
	public static final String LOCALE_RESOLVER_BEAN_NAME = "localeResolver"; 
	public static final String THEME_RESOLVER_BEAN_NAME = "themeResolver"; 
	public static final String HANDLER_MAPPING_BEAN_NAME = "handlerMapping"; 
	public static final String HANDLER_ADAPTER_BEAN_NAME = "handlerAdapter"; 
	public static final String HANDLER_EXCEPTION_RESOLVER_BEAN_NAME = "handlerExceptionResolver";
	public static final String REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME = "viewNameTranslator"; 
	public static final String VIEW_RESOLVER_BEAN_NAME = "viewResolver"; 
	public static final String FLASH_MAP_MANAGER_BEAN_NAME = "flashMapManager"; 
	public static final String WEB_APPLICATION_CONTEXT_ATTRIBUTE = DispatcherServlet.class.getName() + ".CONTEXT"; 
	public static final String LOCALE_RESOLVER_ATTRIBUTE = DispatcherServlet.class.getName() + ".LOCALE_RESOLVER"; 
	public static final String THEME_RESOLVER_ATTRIBUTE = DispatcherServlet.class.getName() + ".THEME_RESOLVER"; 
	public static final String THEME_SOURCE_ATTRIBUTE = DispatcherServlet.class.getName() + ".THEME_SOURCE"; 
	public static final String INPUT_FLASH_MAP_ATTRIBUTE = DispatcherServlet.class.getName() + ".INPUT_FLASH_MAP"; 
	public static final String OUTPUT_FLASH_MAP_ATTRIBUTE = DispatcherServlet.class.getName() + ".OUTPUT_FLASH_MAP"; 
	public static final String FLASH_MAP_MANAGER_ATTRIBUTE = DispatcherServlet.class.getName() + ".FLASH_MAP_MANAGER"; 
	public static final String EXCEPTION_ATTRIBUTE = DispatcherServlet.class.getName() + ".EXCEPTION"; 
	public static final String PAGE_NOT_FOUND_LOG_CATEGORY = "org.springframework.web.servlet.PageNotFound"; 
	private static final String DEFAULT_STRATEGIES_PATH = "DispatcherServlet.properties"; 
	protected static final Log pageNotFoundLogger = LogFactory.getLog(PAGE_NOT_FOUND_LOG_CATEGORY); 
	private static final Properties defaultStrategies; 
	static { 
		try {
			ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, DispatcherServlet.class); 
			defaultStrategies = PropertiesLoaderUtils.loadProperties(resource); 
		}catch (IOException ex) { 
			throw new IllegalStateException("Could not load 'DispatcherServlet.properties': " + ex.getMessage()); 
		} 
	}

	/** Detect all HandlerMappings or just expect "handlerMapping" bean? */ 
	private boolean detectAllHandlerMappings = true; 
	
	/** Detect all HandlerAdapters or just expect "handlerAdapter" bean? */ 
	private boolean detectAllHandlerAdapters = true; 
	
	/** Detect all HandlerExceptionResolvers or just expect "handlerExceptionResolver" bean? */ 
	private boolean detectAllHandlerExceptionResolvers = true; 
	
	/** Detect all ViewResolvers or just expect "viewResolver" bean? */ 
	private boolean detectAllViewResolvers = true; 
	
	/** Throw a NoHandlerFoundException if no Handler was found to process this request? **/ 
	private boolean throwExceptionIfNoHandlerFound = false; 
	
	/** Perform cleanup of request attributes after include request? */ 
	private boolean cleanupAfterInclude = true;
	
	/** MultipartResolver used by this servlet */ 
	private MultipartResolver multipartResolver; 
	
	/** LocaleResolver used by this servlet */ 
	private LocaleResolver localeResolver; 
	
	/** ThemeResolver used by this servlet */ 
	private ThemeResolver themeResolver; 
	
	/** List of HandlerMappings used by this servlet */ 
	private List<HandlerMapping> handlerMappings; 
	
	/** List of HandlerAdapters used by this servlet */ 
	private List<HandlerAdapter> handlerAdapters; 
	
	/** List of HandlerExceptionResolvers used by this servlet */ 
	private List<HandlerExceptionResolver> handlerExceptionResolvers; 
	
	/** RequestToViewNameTranslator used by this servlet */ 
	private RequestToViewNameTranslator viewNameTranslator; 
	
	private FlashMapManager flashMapManager; 
	
	/** List of ViewResolvers used by this servlet */ 
	private List<ViewResolver> viewResolvers; 
	
	public DispatcherServlet() { 
		super(); 
	}
	
	public DispatcherServlet(WebApplicationContext webApplicationContext) { 
		super(webApplicationContext); 
	}
	@Override 
	protected void onRefresh(ApplicationContext context) { 
		initStrategies(context); 
	}
	protected void initStrategies(ApplicationContext context) { 
		initMultipartResolver(context); 
		initLocaleResolver(context); 
		initThemeResolver(context); 
		initHandlerMappings(context); 


ontext context) { 
		initMultipartResolver(context); 
		initLocaleResolver(context); 
		initThemeResolver(context); 
		initHandlerMappings(context); 


Keywords: Java Nginx Back-end Programmer mvc

Added by mistercoffee on Thu, 02 Sep 2021 23:37:18 +0300