Summary of Java classic interview questions: Spring MVC

Time:2022-7-29

1. What is spring MVC?

Spring MVC is a follow-up product of springframework and has been integrated into spring web flow. The spring framework provides a fully functional MVC module for building web applications. Using spring’s pluggable MVC architecture, you can choose to use spring’s spring MVC framework or integrate other MVC development frameworks when using spring for web development. This framework separates model, view and controller, decouples the responsibilities of the web layer, and divides complex web applications into logical parts, simplifying development.

2. What are the components of spring MVC?

  • Front controller dispatcherservlet.
  • Mapping controller handlermapping.
  • Processor controller.
  • Models and views modelandview.
  • View resolver.

3. Talk about the running process of spring MVC?

(1) The user sends a request to the front-end controller dispatcherservlet;

(2) After the dispatcher servlet receives the request, it calls the handler mapping processor mapper to request to obtain the handler;

(3) The processor mapper finds the specific processor handler according to the request URL, generates the processor object and the processor interceptor (if any), and returns them to the dispatcher servlet together;

(4) Dispatcherservlet calls handleradapter processor adapter and requests to execute handler;

(5) Handleradapter calls a specific processor to process business logic through adaptation;

(6) The handler returns to modelandview after execution;

(7) The handleradapter returns the handler execution result modelandview to the dispatcher servlet;

(8) Dispatcherservlet sends modelandview to viewresolver view parser for parsing;

(9) Viewresolver returns the specific view after parsing;

(10) Dispatcherservlet renders the view (that is, fills the view with model data)

(11) Dispatcherservlet responds to users.

4. Advantages of spring MVC:

  • It is based on component technology. All application objects, whether controllers, views, or business objects, are Java components, and are closely integrated with other infrastructure provided by spring
  • It does not depend on servlet API (although the goal is so, it does depend on servlet when it is implemented)
  • You can use any view technology, not just JSP
  • Support the mapping strategy of various request resources

5. What is the role of @requestmapping?

Map the HTTP request to the corresponding class / method.

6. If I want to intercept the method submitted by get in the interception request, how to configure it?

You can add method=requestmethod.get to the @requestmapping annotation, or use @getmapping.

7. What are the common annotations of springmvc?

@Requestmapping: an annotation used to handle the mapping of request URLs, which can be used on classes or methods. If it is used on a class, it means that all methods in the class that respond to requests take this address as the parent path.

@Requestbody: the annotation implements the JSON data received from HTTP requests and converts JSON into Java objects.

@ResponseBody: the annotation implementation converts the object returned by the conreoller method into a JSON object to respond to the customer.

8. Which annotation is generally used for the controller in spingmvc? Is there any other annotation that can be replaced?

The @conntroller annotation indicates that it is a presentation layer and cannot be replaced by other annotations.

9. What are the differences between spring MVC and Struts2?

  • The entry of springmvc is a servlet, that is, dispatchservlet, while the entry of Struts2 is a filter filter (strutsprepareandexecutefilter).
  • Springmvc is based on method development (one URL corresponds to one method). The formal parameters of the request parameters passed to the method can be designed as single instance or multiple instances (single instance is recommended). Struts2 is based on class development. The parameters passed are through the properties of the class, and can only be designed as multiple instances.
  • Struts uses the value stack to store the request and response data, and accesses the data through ognl. Springmvc parses the request content through the parameter parser, assigns values to the method formal parameters, encapsulates the data and view into modelandview objects, and finally transmits the model data in modelandview to the page through the requests field. JSP view parser uses JSTL by default.

10. How to get a request or session in the method

Declare the request in the formal parameter of the method, and springmvc will automatically pass in the request object

Recommended Today

JS generate guid method

JS generate guid method https://blog.csdn.net/Alive_tree/article/details/87942348 Globally unique identification(GUID) is an algorithm generatedBinaryCount Reg128 bitsNumber ofidentifier , GUID is mainly used in networks or systems with multiple nodes and computers. Ideally, any computational geometry computer cluster will not generate two identical guids, and the total number of guids is2^128In theory, it is difficult to make two […]