JavaEE architecture

Second, the MVC design pattern | thought

Model layer (javaBean component = Domain Model (javaBean + business layer + persistence layer) View layer (HTML, JSP...) Controller Control layer (delegates data processing to the model layer)Copy the code

Introduction to SpringMVC

Springmvc is a Web layer MVC framework, similar to Struts2.Copy the code

The relationship between SpringMVC and Spring

Springmvc is a part of Spring. In fact, Spring provides MVC modules for Web applications on the original basis.Copy the code

Compare SprigMVC with Struts2

Implementation mechanism: Struts2 is implemented based on filters. Springmvc is implemented based on servlets. Speed: SpringMVC runs slightly faster than Structs2 because the underlying filter is a servlet. Struts2 is parameter encapsulation for multi-instance SpringMVC singletons: Struts2 parameter encapsulation is attribute based encapsulation. Springmvc is based on method encapsulation. Finer granularity.Copy the code

The working principle diagram of SpringMVC

Springmvc specific process steps

⑴ User sends the request to DispatcherServlet. (2) When the DispatcherServlet receives the request, it invokes HandlerMapping to query the specific Handler. HandlerMapping finds the specific processor (the implementation class of which processor is configured), generates the processor object and the processor interceptor (HandlerExcutorChain contains the Handler and interceptor collection) and returns it to the DispatcherServlet. (4) After the DispatcherServlet receives the HandlerExcutorChain returned by the HandlerMapping, it calls the HandlerAdapter to request the execution of the specific Handler(Controller). ⑸ The HandlerAdapter ADAPTS to invoke a specific Handler(Controller, the back-end Controller). The Controller returns the ModelAndView, which contains the logical view and data, to the HandlerAdaptor. ⑺ HandlerAdaptor returns the ModelAndView to the DispatcherServlet. ⑻ DispatcherServlet requests the ViewReslover to resolve the ModelAndView. ⑼ ViewReslover After the analysis, the specific View(physical View) is returned to the DispatcherServlet. We will DispatcherServlet request the render View (populate the View with model data) to render the View according to the View. 11 returns the rendered view to the DispatcherServlet. (12) The DispatcherServlet returns the response result to the user.Copy the code

8. Springmvc Core components

(1) Front-end controller DispatcherServlet (configuration can be) function: central processing unit, receive requests, do not do any processing, but send requests to other components for processing. The DispatcherServlet is the control center for the entire process. (2) HandlerMapping function: search Handler according to url request path sent by DispatcherServlet Common processor mapper: BeanNameUrlHandlerMapping SimpleUrlHandlerMapping, ControllerClassNameHandlerMapping DefaultAnnotationHandlerMapping (not recommended), (3) processor adapter HandlerAdapter (configuration) Function: Executes the Handler according to a specific rule (the rule required by the HandlerAdapter). The processor is executed through a HandlerAdapter, which is an application of the adapter pattern, extending multiple adapters to execute on more types of processors. Common processor adapter: HttpRequestHandlerAdapter SimpleControllerHandlerAdapter, AnnotationMethodHandlerAdapter (4) processor Handler called Controller (ape to write) functions: write the Handler to do, according to the requirements of HandlerAdapter this adapter can Handler to correct execution. (5) View parser ViewReslover(configurable) function: view parsing, according to the logical view name parsing into a real view. The ViewResolver is responsible for generating the processing result into a View. The ViewResolver first parses the logical View name into a physical View name, i.e. a specific page address, and generates a View object. Finally, the ViewResolver renders the View and presents the processing result to users through the page. The SpringMVC framework provides a variety of View types, such as jstlView, freemarkerView, pdfView... View is an interface that implements classes that support different View types (JSP, freemarker, PDF...).Copy the code

Introduction to SpringMVC

  • Create a Maven project
  • Configuration of pom. The XML

Introduce dependencies: spring base package, Spring-WebMVC required by SpringMVC, slF4J-log4j12 for logging, JSTL for JSP, servlet-API, JSP-API.

  • Configure web. XML

Because the DispatcherServlet is itself a Servlet, it needs to be configured in web.xml.

  • Configuration for springmvc. XML

The default way to load the SpringMVC configuration file must follow the following specifications:

① Naming rule :-servlet. XML ====> springmvc-servlet.xml

– Servlet. XML must be placed under WEB-INF

2. If you do not follow the default loading location, you need to specify the loading path of the SpringMVC configuration file in the web.xml tag, as shown in the figure above.

  • Custom Controller(processor)

The custom Controller CPU configuration by the spring container to manage in the spring container, because for springmvc here. The XML configuration file processor mapper is BeanNameUrlHandlerMapping configuration, The Handler mapper looks for the execution class Handler(Controller) based on the bean’s (custom Controller) name property value URL, so the bean’s name property value is the URL to match the request path sent by the user.

  • Define the view page

Resolve the path according to the view: web-INF/JSPS /index.jsp

  • Analyze the SpringMVC execution flow according to the code

  • Processor mapper (configure multiple processor mappers to coexist)
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
Copy the code

Find the implementation Controller class based on the URL of the bean’s name property.

  • Processor adapters (configure multiple processor adapters to coexist)
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
Copy the code

Functionality: When a custom processor (Controller) implements the Controller interface, the adapter executes the Controller’s specific methods.

  • Processor adapter source SimpleControllerHandlerAdapter

The Controller interface has a method called handleRequest, which is the handler method.

Therefore, in order for a custom Controller to be called, it must implement the Controller interface and override the processor methods in the Controller interface.

  • Add the log

  • The results

If you think this article is helpful to you, please help to like or follow it. It is a good encouragement for me. Thank you in advanceCopy the code