Overview What is Spring MVC? What is your understanding of Spring MVC? Benefits of Spring MVC Core components Main components of Spring MVC? What is DispatcherServlet and what is the Spring MVC framework controller? Is the controller of Spring MVC singleton? If so, what is the problem and how to solve it? How does it work please describe the workflow of Spring MVC? Describe the workflow of DispatcherServlet? MVC framework what is MVC? What are the benefits of the MVC design pattern? What are the common annotations? What are the common annotations for Spring MVC? What is the general annotation for the controller in SpingMvc? Is there any other annotation that can replace it? @requestMapping @responseBody @PathVariable vs. @requestParam Other Spring MVC vs. Struts2 Spring MVC vs. Struts2 How does MVC set up redirection and forwarding? How do Spring MVC and AJAX call each other? How to solve the POST request Chinese garble problem, how to handle GET? Spring MVC exception handling? If I’m intercepting a Request, and I want to intercept a get submission method, how do I configure how do I get a Request or a Session in a method? If you want to get arguments from the foreground in an intercepting method, how do you get them? If there are many arguments passed to the foreground, and they are all of an object, how do you get the object quickly? What is the return value of a function in Spring MVC? What object does Spring MVC use to pass data from the background to the foreground? How to put ModelMap data into Session? WebApplicationContext (Java, Java, Java, Java, Java, Java, Java, Java) Contains most of the knowledge you need or can use in an interview as a Java engineer.

Overview What is Spring MVC? What is your understanding of Spring MVC? Spring MVC is a java-based request-driven Web framework that implements MVC design pattern. By separating model-view-controller, the Web layer decoups responsibilities, and divides complex Web applications into logically clear parts, simplifying development, reducing errors, and facilitating cooperation among developers within the group.

The benefits of Spring MVC (1) support a variety of view technologies, not just JSPS;

(2) Integration with Spring framework (such as IoC container, AOP, etc.);

(3) clear role allocation: front-end controller (dispatcherServlet), request to processor mapping (handlerMapping), processor adapter (HandlerAdapter), ViewResolver (ViewResolver).

(4) Support the mapping strategy of various requested resources.

Core components The main components of Spring MVC? (1) Front-end controller DispatcherServlet (no programmer development required)

Function: receive request and response results, equivalent to a forwarder, with DispatcherServlet to reduce the coupling degree between other components.

(2) Processor mapping HandlerMapping (no programmer development required)

Finds the Handler based on the requested URL

(3) HandlerAdapter

Note: When writing a Handler, follow the rules specified by the HandlerAdapter so that the adapter HandlerAdapter can properly execute the Handler.

(4) Processor Handler (programmer development required)

(5) ViewResolver (no programmer development required)

Function: Parse a view into a real view according to its logical name

(6) View View (need programmer to develop JSP)

View is an interface whose implementation classes support different View types (JSP, Freemarker, PDF, etc.)

Spring’s MVC framework is designed around DispatcherServlet, which handles all HTTP requests and responses.

What is the Spring MVC framework controller? A controller provides a behavior to access an application, which is typically implemented through a service interface. The controller parses user input and transforms it into a model that the view presents to the user. Spring implements a control layer in a very abstract way, allowing users to create controllers for multiple purposes.

Is the controller of Spring MVC singleton? If so, what is the problem and how to solve it? A: It is a singleton mode, so there are thread safety issues when multi-threaded access, do not use synchronization, will affect performance, the solution is to write fields in the controller.

How does it work please describe the workflow of Spring MVC? Describe the workflow of DispatcherServlet? (1) The user sends the request to the front-end controller DispatcherServlet; (2) After receiving the request, the DispatcherServlet invokes the HandlerMapping processor mapper to request to obtain the Handle; (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 the DispatcherServlet; (4) DispatcherServlet calls HandlerAdapter processor adapter; (5) The HandlerAdapter ADAPTS to call the specific Handler (Handler, also called the back-end controller); (6) The Handler returns ModelAndView after execution; (7) HandlerAdapter returns the Handler execution result ModelAndView to DispatcherServlet; (8) DispatcherServlet sends ModelAndView to ViewResolver view parser for parsing; (9) The ViewResolver returns the concrete View after parsing; (10) The DispatcherServlet renders the View (that is, populates the View with model data). (11) The DispatcherServlet responds to the user.

MVC framework what is MVC? MVC is a design pattern (a design pattern is a good way to write code in daily development and a summary of experience). Model – View – Controller: the design mode of three-tier architecture. It is used to realize the separation of front-end page display and back-end business data processing.

Benefits of the MVC design pattern

1. The hierarchical design realizes the decoupling between the components of the business system, which is conducive to the scalability and maintainability of the business system.

2. It is conducive to the parallel development of the system and improves the development efficiency.

Annotations are essentially a special interface that inherits annotations, implemented by dynamic proxy classes generated by the Java runtime. When we get annotations through reflection, we return a dynamic proxy object generated by the Java runtime. Through a proxy object call the custom annotation method, will eventually call AnnotationInvocationHandler invoke method. This method indexes the corresponding value from the Map memberValues. The source of memberValues is the Java constant pool.

What are common Spring MVC annotations? RequestMapping: annotation used to handle requested URL mapping, which can be used on a class or method. For a class, that means that all methods in the class that respond to requests have that address as the parent path.

@RequestBody: Annotation implementation receives JSON data from HTTP requests and converts json to Java objects.

ResponseBody: The annotation implementation converts the object returned by the Conreoller method into a JSON object response to the client.

What is the general annotation for the controller in SpingMvc? Is there any other annotation that can replace it? A: The @Controller annotation is usually used, but the @RestController annotation is also used. The @restController annotation is equivalent to @responseBody + @Controller, indicating that it is the presentation layer.

In Spring MVC, the Controller Controller is responsible for handling requests distributed by DispatcherServlet. It encapsulates the data requested by the user into a Model after being processed by the business processing layer. The Model is then returned to the corresponding View for display. Spring MVC provides a very easy way to define a Controller. You don’t have to inherit a particular class or implement a particular interface. You just use @Controller to mark a class as Controller, Then use @requestMapping and @RequestParam annotations to define the mapping between the URL request and the Controller method so that the Controller can be accessed. In addition, Controller does not rely directly on HttpServlet objects such as HttpServletRequest and HttpServletResponse, which can be obtained flexibly through Controller method parameters.

@Controller is used to mark a class, and the class it marks is a Spring MVC Controller object. The distribution handler will scan the method of the class that uses the annotation and check if the method uses the @RequestMapping annotation. @Controller just defines a Controller class, and methods using the @RequestMapping annotation are the handlers that actually handle requests. Just using the @Controller tag on a class doesn’t really make it a Spring MVC Controller class, because Spring doesn’t know it yet. So how do you do Spring to recognize it? At this point we need to hand over the controller class to Spring to manage. There are two ways:

Define the Bean object for MyController in the Spring MVC configuration file. In the Spring MVC configuration file, tell Spring where to find the Controller Controller labeled @Controller. RequestMapping is an annotation that handles request address mapping. RequestMapping can be used on a class or method. Used on a class, this address is used as the parent path for all methods in the class that respond to requests.

The RequestMapping annotation has six attributes, which we’ll break down into three categories (see examples below).

The value and method

Value: Specifies the actual address of the request, which can be the URI Template pattern (described below).

Method: Specifies the request method type, such as GET, POST, PUT, and DELETE.

Consumes, produces

Consumes: Specifies the content-types that handle requests, for example, application/ JSON or text/ HTML.

Produces: Specifies the content type returned only if the specified type is in the (Accept) type in the Request header.

Params, headers

Params: Specifies that the request must contain some parameter values for this method to process.

Headers: Specifies that the request must contain some specified header value before the method can process the request.

The @responseBody annotation is used to write the object returned by Controller’s methods to the body data section of the Response object after converting it to the specified format via the appropriate HttpMessageConverter.

When to use: when the data returned is not HTML tag pages, but some other format of data (such as JSON, XML, etc.);

The difference between @pathVariable and @requestParam is that the request path has an ID variable value, @pathVariable (value = “/page/{id}”, method = requestmethod.get)

RequestParam is used to get static URL requests into spring annotations for action.

Other Spring MVC differences are similar to Struts2

Both are MVC based presentation layer frameworks for Web project development.

The difference between

1. The front-end controller is different. The front-end controller of Spring MVC is the servlet: DispatcherServlet. Struts 2 front controller is a filter: StrutsPreparedAndExcutorFilter.

2. Request parameters are received in different modes. Spring MVC uses method parameters to receive request parameters. Method-based development, thread-safe, can be designed for singleton or multi-case development. Singleton development is recommended (more efficient). Struts2 is a class based development that receives the parameters of the request through the member variables of the class. It is not thread safe and can only be designed for multi-case development.

3.Struts uses value stack to store the data of request and response, and accesses data through OGNL. Spring MVC parses the request content through parameter parser, assigns values to method parameters, and encapsulates data and view into ModelAndView objects. Finally, the model data in the ModelAndView is transferred to the page via the RequES domain. The Jsp view parser uses JSTL by default.

4. Different from Spring integration. Spring MVC is part of the Spring framework and does not require integration. In enterprise projects, Spring MVC is used more often.

How does Spring MVC set up redirection and forwarding? (1) Forward: add “forward:” in front of the return value, such as “forward:user.do? name=method4”

(2) Redirect: add “redirect:” to the return value, e.g. “redirect:www.baidu.com”

How do Spring MVC and AJAX call each other? Using the Jackson framework, you can convert Java objects directly into Json objects that Js can recognize. The specific steps are as follows:

(1) Add jackson. jar

(2) Configure the JSON mapping in the configuration file

(3) You can return an Object, a List, etc., with the @responseBody annotation in front of the method.

How to solve the POST request Chinese garble problem, how to handle GET? (1) Solve the post request garble problem:

Configure a CharacterEncodingFilter in web. XML and set it to UTF-8.

<init-param>
    <param-name>encoding</param-name>
    <param-value>utf-8</param-value>
</init-param>
Copy the code

① Modify the Tomcat configuration file to add the code consistent with the project code as follows:

<ConnectorURIEncoding=” UTF-8 “connectionTimeout=”20000″ port=”8080″ protocol=”HTTP/1.1” redirectPort=”8443″/> 1 ② Another method to re-encode parameters:

String userName = new String(request.getParamter(” userName “).getBytes(” ISO8859-1 “), “UTF-8”)

Iso8859-1 is the default Tomcat encoding. You need to use UTF-8 encoding to encode the tomcat encoding.

Spring MVC exception handling? A: You can throw exceptions to the Spring framework, which handles them; We just need to configure a simple exception handler and add a view page to the exception handler.

Requestmethod.get (@requestMethod.get, @requestMethod.get, @requestMethod.get, @requestMethod.get)

How do I get a Request or a Session in a method? A: Declare request directly in a method parameter, and Spring MVC automatically passes in the Request object.

If you want to get arguments from the foreground in an intercepting method, how do you get them? A: Declare the parameter in the parameter, but only with the same name as the passed parameter.

If there are many arguments passed to the foreground, and they are all of an object, how do you get the object quickly? A: Declare the object directly in a method, and Spring MVC automatically assigns properties to the object.

What is the return value of a function in Spring MVC? A: Return values can have many types, including String, ModelAndView. The ModelAndView class merges views and data together, but strings are usually better.

What object does Spring MVC use to pass data from the background to the foreground? A: With ModelMap objects, you can call the PUT method in this object, add the object to it, and the foreground can get it through the EL expression.

How to put ModelMap data into Session? A: You can add the @sessionAttributes annotation on top of the class, which contains a string that is the key to put into the session.

There are two ways to write interceptors in Spring MVC. One is to implement the HandlerInterceptor interface, and the other is to inherit the adapter class, and then implement the processing logic in the interface method. Then configure the interceptor in the Spring MVC configuration file:

mvc:interceptors mvc:interceptor
1 2 3 4 5 6 WebApplicationContext WebApplicationContext inherits ApplicationContext and adds some unique features required for WEB applications. It differs from the generic ApplicationContext in that it can process the subject and find the servlet associated with it. ———————————————— Copyright notice: This article is originally published BY CSDN blogger “ThinkWon” under CC 4.0 BY-SA copyright agreement. Please attach the link of the original source and this statement. The original link: blog.csdn.net/ThinkWon/ar…