1. What is Spring MVC? A little bit about your understanding of springMVC?

Spring MVC is a lightweight Web framework based on Java that implements the request-driven type of MVC design pattern. By separating Model, View and Controller, the Web layer decouplesresponsibilities, and divides complex Web applications into several logically clear parts, simplifying development and reducing errors. Facilitate collaboration between developers within the group.

2. SpringMVC flow?

The user sends the request to the front-end controller DispatcherServlet;

After receiving the request, the DispatcherServlet invokes the HandlerMapping processor mapper to request to obtain the Handle.

The processor mapper finds the specific processor based on the request URL and generates both the processor object and the processor interceptor (if any) back to the DispatcherServlet;

DispatcherServlet calls the HandlerAdapter processor adapter;

A HandlerAdapter ADAPTS to call a specific Handler (Handler, also called a back-end controller);

Handler completes execution and returns ModelAndView;

The HandlerAdapter returns the Handler execution result ModelAndView to the DispatcherServlet;

The DispatcherServlet passes the ModelAndView to the ViewResolver view parser for parsing;

The ViewResolver is parsed and returns a concrete View.

The DispatcherServlet renders the View (populate the View with model data)

DispatcherServlet responds to the user.

3. Advantages of Springmvc:

Support for a variety of view technologies, not just JSPS;

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

Clear role assignment: front-end controller (dispatcherServlet), request-to-handler mapping (HandlerAdapter), ViewResolver (ViewResolver).

Supports mapping strategies for various requested resources.

What are the main components of Spring MVC?

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.

HandlerMapping (no programmer development required)

Finds the Handler based on the requested URL

Processor adapter HandlerAdapter

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

Handler (programmer development required)

ViewResolver (no programmer development required)

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

View View (requires a programmer to develop a JSP)

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

What are the differences between springMVC and Struts2?

For springmvc entry is a servlet that front controller (DispatchServlet), and struts 2 is a filter blocking entrance (StrutsPrepareAndExecuteFilter).

Springmvc is method-based development (one URL corresponds to one method). Request parameters are passed to method parameters, which can be designed as singletons or multiple instances (suggested singletons). Struts2 is class-based development, passing parameters through class attributes, which can only be designed as multiple instances.

Struts uses value stack to store the data of request and response, and accesses data through OGNL. Springmvc 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.

How does SpringMVC set redirection and forwarding?

Forward: Prefix the return value with “forward:”, such as “forward:user.do? name=method4”

Redirect: Add “redirect:” to the return value, such as “redirect:www.baidu.com”

How do SpringMvc 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:

Join Jackson. The jar

Configure the JSON mapping in the configuration file

An Ajax method can return Object,List, etc., with the @responseBody annotation in front of it.

8, how to solve the POST request Chinese garble problem, and how to deal with GET?

Resolve post request garbled characters:

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

CharacterEncodingFilterorg.springframework.web.filter.CharacterEncodingFilterencodingutf-8CharacterEncodingFilter/*

There are two solutions to the problem:

Modify the code of the Tomcat configuration file to be consistent with the project code as follows:

<ConnectorURIEncoding=” UTF-8 “connectionTimeout=”20000″ port=”8080″ protocol=”HTTP/1.1” redirectPort=”8443″/>

There is another way to re-encode the 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.

Is SpringMvc’s controller a singleton? If so, what are the problems and how to solve them?

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.

11. What are the common annotations used by SpringMVC?

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.

12. Controller annotations in SpingMvc are generally usedtheIs there any other annotation to replace it?

A: The @conntroller annotation is usually used to indicate the presentation layer and cannot be replaced by other annotations.

13. What if I want to intercept get methods in a request interception?

A: You can add method= requestMethod.get to @requestMapping.

How do I get a Request or Session from a method?

A: Declare request directly in a method parameter, and SpringMvc automatically passes in the Request object.

15. If you want to get an argument passed from the foreground in an intercepting method, how do you get it?

A: Declare the parameter in the parameter, but only with the same name as the passed parameter.

16, If the foreground has many parameters, and these parameters are an object, then how to quickly get the object?

A: Declare the object directly in a method, and SpringMvc automatically assigns properties to the object.

What is the return value of a SpringMvc function?

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 SpringMvc use to pass data from the background to the front desk?

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.

19. 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.

How to write interceptors in SpringMvc:

There are two ways to write, one is to implement the HandlerInterceptor interface, the other is to inherit the adapter class, and then in the interface method, implement the processing logic; Then configure the interceptor in the SpringMvc configuration file:

21, annotation principle:

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.

! [](https://upload-images.jianshu.io/upload_images/23974063-d56abc05dc4d46ce? imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
! [](https://upload-images.jianshu.io/upload_images/23974063-98fa1967e8404ec6? imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

I get the full text of the MNC interview question!!