编程知识 cdmana.com

The latest spring MVC interview questions in 2021

Recommend using small program to read

In order to make it more convenient for you to read
All the interview questions in this article have been arranged into a small program 《 Interview manual
It can be scanned through wechat ( Or long press ) Enjoy a better reading experience with the QR code below !

ah_xct


Catalog
  • Recommend using small program to read
  • 1. Spring MVC Basics
    • 1.1 What is? Spring MVC?
    • 1.2 Spring MVC What are the advantages of ?
    • 1.3 Spring MVC The main components of ?
    • 1.4 What is? DispatcherServlet?
    • 1.5 What is? Spring MVC Frame controller ?
    • 1.6 Spring MVC Is this controller singleton ?
    • 1.7 Spring MVC What's the problem with the singleton controller ? How to deal with it ?
    • 1.8 Briefly DispatcherServlet workflow
    • 1.9 MVC What is it? ?
    • 1.10 MVC What are the benefits of design patterns ?
    • 1.11 Spring MVC And Struts2 difference ?
    • 1.12 WebApplicationContext What's the role ?
  • 2. Spring MVC annotation
    • 2.1 What is the annotation principle ?
    • 2.2 Spring MVC What are the common annotations ?
    • 2.3 Sping MVC What is the controller annotation in ?
    • 2.4 @Controller The role of annotations
    • 2.5 @RequestMapping The role of annotations ?
    • 2.6 @ResponseBody What is the function of annotation ?
    • 2.7 @PathVariable and @RequestParam The difference between ?
  • 3. Spring MVC Application
    • 3.1 Spring MVC How to set redirection and forwarding ?
    • 3.2 Spring MVC How and AJAX Calling each other ?
    • 3.3 Spring MVC How to solve GET、POST Request Chinese garbled code problem ?
    • 3.4 Spring MVC How to handle exceptions ?
    • 3.5 Spring MVC How to intercept GET request ?
    • 3.6 How to get Request, perhaps Session?
    • 3.7 Spring MVC How to get request parameters ?
    • 3.8 SpringMVC How to receive multiple parameters gracefully ?
    • 3.9 Spring MVC What is the return value of the function in ?
    • 3.10 Spring MVC What object is used to transfer data from the background to the front desk ?
    • 3.11 How to handle ModelMap Put the data in Session Inside ?
    • 3.12 Spring MVC How the interceptor works ?
  • 4. other
    • 4.1 SpringMVC How to layer the system in the system ?


1. Spring MVC Basics

1.1 What is? Spring MVC?

Spring MVC It's based on Java Realized. MVC Design pattern request driven type lightweight Web frame , By putting the model - View - Controller separation , take web Layer to decouple responsibilities , Put the complicated web The application is divided into logical parts , Simplify the development , Reduce errors , Facilitate the cooperation between developers in the group .

1.2 Spring MVC What are the advantages of ?

  1. It can support various view technologies , Not limited to JSP;
  2. And Spring Framework for the integration ( Such as IoC Containers 、AOP etc. );
  3. Clear role assignment :
    • Front controller (dispatcherServlet) ;
    • Request to processor mapping (handlerMapping);
    • Processor adapter (HandlerAdapter);
    • view resolver (ViewResolver).
  4. Support the mapping strategy of various request resources .

1.3 Spring MVC The main components of ?

  1. DispatcherServlet:
    The central controller , Forward the request to the specific control class
  2. Controller:
    The specific controller to handle the request
  3. HandlerMapping:
    Mapping processor , Responsible for mapping CPU forwarding to controller The mapping strategy of time
  4. ModelAndView:
    Data returned by service layer and encapsulation class of view layer
  5. ViewResolver:
    view resolver , Parse specific views
  6. Interceptors :
    Interceptor , Be responsible for intercepting the request defined by us and doing the processing work

1.4 What is? DispatcherServlet?

Spring Of MVC The framework is around DispatcherServlet To design the , It's used to handle everything HTTP Requests and responses .

1.5 What is? Spring MVC Frame controller ?

The controller provides a behavior to access the application , This behavior is usually implemented through the service interface . The controller parses the user input and transforms it into a model presented to the user by the view .Spring Implemented a control layer in a very abstract way , Allows users to create multi-purpose controllers .

1.6 Spring MVC Is this controller singleton ?

Is a singleton .

1.7 Spring MVC What's the problem with the singleton controller ? How to deal with it ?

  • problem
    There are thread safety problems when multithreading ;
  • Solution
    You can't write fields in the controller .

1.8 Briefly DispatcherServlet workflow

  1. User sends request to front controller DispatcherServlet;
  2. DispatcherServlet Upon receipt of the request , call HandlerMapping Processor mapper , The request for Handle;
  3. Processor mapper on request url Find the specific processor , Generating processor objects and interceptors ( Generate if any ) Return to DispatcherServlet;
  4. DispatcherServlet call HandlerAdapter Processor adapter ;
  5. HandlerAdapter After adaptation call Specific processor (Handler, Also called back end controller );
  6. Handler Execution complete return ModelAndView;
  7. HandlerAdapter take Handler Execution results ModelAndView Return to DispatcherServlet;
  8. DispatcherServlet take ModelAndView Pass to ViewResolver View parser for parsing ;
  9. ViewResolver Return details after parsing View;
  10. DispatcherServlet Yes View Render view ( Fill model data into view )
  11. DispatcherServlet Responding to users .

1.9 MVC What is it? ?

MVC It's a design pattern :

  • Model (model)
  • View (view)
  • controller (controller)

The design pattern of three-tier architecture . It is used to separate the display of front-end page and back-end business data processing .

1.10 MVC What are the benefits of design patterns ?

  1. Layered design , The decoupling between the components of business system is realized , It is conducive to the scalability of business system , Maintainability .

  2. It is conducive to the parallel development of the system , Improve development efficiency .

1.11 Spring MVC And Struts2 difference ?

  • The same thing

    It's all based on mvc The presentation layer framework of , All for web Project development .

  • Difference

    1. Front end controllers are different .Spring MVC The front controller of is servlet:DispatcherServlet.struts2 The front controller of is filter:StrutsPreparedAndExcutorFilter.

    2. Request parameters are received differently .Spring MVC Is to use the formal parameters of the method to receive the parameters of the request , Method based development , Thread safety , It can be designed for single instance or multi instance development , The development of singleton mode is recommended ( More efficient execution ), The default is singleton development mode .struts2 Is to receive the requested parameters through the member variables of the class , It's class based development , Thread unsafe , Can only be designed for multi instance development .

    3. Struts A value stack is used to store the data for the request and response , adopt OGNL Access data ,Spring MVC Through the parameter parser is will request Request content resolution , And assign values to method parameters , Encapsulate data and views into ModelAndView object , Then will ModelAndView The model data is passed reques The domain is transferred to the page .Jsp View parser by default jstl.

    4. And spring Integration is different .Spring MVC yes spring Part of the frame , No integration required . In enterprise projects ,Spring MVC Use more .

1.12 WebApplicationContext What's the role ?

WebApplicationContext Inherited ApplicationContext And added some WEB Unique functions necessary for application , It's different from the general ApplicationContext , Because it can handle topics , And find the associated servlet.

2. Spring MVC annotation

2.1 What is the annotation principle ?

The essence of annotation is an inheritance Annotation Special interface for , Its concrete implementation class is Java Dynamic agent classes generated at run time . When we get annotations by reflection , The return is Java Dynamic proxy objects generated at run time . Call the method of custom annotation through proxy object , Will eventually call AnnotationInvocationHandler Of invoke Method . The method will memberValues This Map The middle line leads to the corresponding value . and memberValues The source of this is Java Constant pool .

2.2 Spring MVC What are the common annotations ?

  • @RequestMapping
    Used to process requests url Comments on the mapping , Can be used on a class or method . For classes , It means that all the methods in the class responding to the request take this address as the parent path .

  • @RequestBody
    Annotation implementation receive http Requested json data , take json Convert to java object .

  • @ResponseBody
    The annotation implementation will conreoller Method returns the object to json Object responds to customer .

2.3 Sping MVC What is the controller annotation in ?

It's usually used @Controller annotation ;
You can also use @RestController

@RestController Annotation equivalent @ResponseBody + @Controller

2.4 @Controller The role of annotations

stay Spring MVC in , controller Controller Responsible for handling by DispatcherServlet Requests for distribution , It encapsulates the data requested by the user into one after being processed by the business processing layer Model , And then Model Return to the corresponding View To display . stay Spring MVC A very simple definition is provided in Controller Methods , You don't have to inherit specific classes or implement specific interfaces , Just use @Controller Mark a class as Controller , And then use @.........

版权声明
本文为[Program ape Owen]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/01/20210131192200013j.html

Scroll to Top