Summary of SpringMVC common interview questions (super detailed answer reprinted)

1. What is Spring MVC? Briefly introduce your understanding of springMVC?

Spring MVC is a Java-based lightweight web framework that implements the request-driven type of the MVC design pattern. By separating Model, View, and Controller, the web layer is decoupled from responsibilities, and complex web applications are divided into logically clear sections. Part, simplify development, reduce errors, and facilitate cooperation among developers in the group.

 

2. What is the process of SpringMVC?

(1) The user sends a request to the front controller DispatcherServlet;
(2) After receiving the request, the DispatcherServlet calls the HandlerMapping processor mapper to request the Handle;
(3) The processor mapper finds the specific processor according to the request url, and generates processing The handler object and the handler interceptor (generated if there is one) are returned to DispatcherServlet together;
(4) DispatcherServlet calls the HandlerAdapter processor adapter;
(5) HandlerAdapter calls the specific handler (Handler, also called back-end controller) after adaptation ;
(. 6) perform complete Handler returns ModelAndView;
(. 7) Handler HandlerAdapter is the execution result is returned to ModelAndView the DispatcherServlet;
(. 8) to the DispatcherServlet ModelAndView pass parser parses ViewResolver view;
return after DETAILED view (. 9) analytical ViewResolver;
(10 ) DispatcherServlet renders the View (that is, fills the model data into the view)
(11) DispatcherServlet responds to the user.

3. Advantages of Springmvc:

(1) It can support various view technologies, not just limited to JSP;

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

(3) Clear role assignment: front controller (dispatcherServlet), request to handler mapping (handlerMapping), handler adapter (HandlerAdapter), view resolver (ViewResolver).

(4) Support mapping strategies for various requested resources.

 

4. The main components of Spring MVC?

(1) DispatcherServlet front controller (no programmer development required)

Function: Receive requests and response results, which are equivalent to repeaters. With DispatcherServlet, the coupling between other components is reduced.

(2) HandlerMapping (no programmer development required)

Role: according to the requested URL to find the Handler

(3) HandlerAdapter

Note: When writing the Handler, it must be written according to the rules required by the HandlerAdapter, so that the HandlerAdapter can execute the Handler correctly.

(4) Handler (need to be developed by programmers)

(5) View Resolver ViewResolver (no programmer development required)

Function: Analyze the view and resolve it into a real view according to the logical name of the view

(6) View (requires programmer to develop jsp)

View is an interface, and its implementation class supports different view types (jsp, freemarker, pdf, etc.)

 

5. What are the differences between springMVC and struts2?

(1) The entrance of springmvc is a servlet that is a front controller (DispatchServlet), and the entrance of struts2 is a filter (StrutsPrepareAndExecuteFilter).

(2) Springmvc is based on method development (one url corresponds to one method), the request parameters are passed to the formal parameters of the method, which can be designed as singleton or multiple cases (singleton is recommended), struts2 is developed based on classes, and the parameters are passed through classes The attribute can only be designed for multiple cases.

(3) 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 parameters, encapsulates the data and views into ModelAndView objects, and finally Transfer the model data in ModelAndView to the page through the reques field. The Jsp view parser uses jstl by default.

 

6. How does SpringMVC set redirection and forwarding?

(1) Forwarding: add "forward:" in front of the return value, such as "forward:user.do?name=method4"

(2) Redirection: add "redirect:" in front of the return value, such as "redirect:http://www.baidu.com"

 

7. How do SpringMvc and AJAX call each other?

Through the Jackson framework, the objects in Java can be directly converted into Json objects that Js can recognize. Specific steps are as follows:

(1) Join Jackson.jar

(2) Configure json mapping in the configuration file

(3) You can directly return Object, List, etc. in the receiving Ajax method, but the @ResponseBody annotation must be added in front of the method.

 

8. How to solve the problem of garbled Chinese characters in POST request, and how to deal with GET?

(1) Solve the problem of garbled post request:

Configure a CharacterEncodingFilter filter in web.xml and set it to utf-8;

<filter>

    <filter-name>CharacterEncodingFilter</filter-name>

    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

    <init-param>

        <param-name>encoding</param-name>

        <param-value>utf-8</param-value>

    </init-param>

</filter>

<filter-mapping>

    <filter-name>CharacterEncodingFilter</filter-name>

    <url-pattern>/*</url-pattern>

</filter-mapping>

(2) There are two solutions to the garbled characters in the Chinese parameter of the get request:

① Modify the tomcat configuration file and add the code to be consistent with the project code, as follows:

<ConnectorURIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

 ②Another method to re-encode the parameters:

String userName = new String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8")

ISO8859-1 is the default encoding of tomcat, and the content encoded by tomcat needs to be encoded according to utf-8.

 

9. What is the exception handling of Spring MVC?

Answer: The exception can be thrown to the Spring framework and handled by the Spring framework; we only need to configure a simple exception handler and add a view page to the exception handler.

10. Is the controller of SpringMvc singleton mode? If so, what is the problem and how to solve it?

Answer: It is a singleton mode, so there are thread safety issues during multithreaded access. Do not use synchronization, which will affect performance. The solution is that you cannot write fields in the controller.

11. What are the annotations commonly used in SpringMVC?

@RequestMapping: An annotation used to process request url mapping, which can be used on classes or methods. Used on a class, it means that all methods in the class that respond to requests use this address as the parent path.

@RequestBody: Annotation realizes receiving json data of http request and converting json into java object.

@ResponseBody: The annotation implementation converts the object returned by the conreoller method into a json object response to the customer.

12. The comment of the controller in SpingMvc generally uses that. Is there any other comment that can be substituted?

Answer: Generally use @Controller annotation, you can also use @RestController, @RestController annotation is equivalent to @ResponseBody + @Controller, which means it is the presentation layer. In addition, it is generally not replaced by other annotations.

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

Answer: You can add method=RequestMethod.GET in the @RequestMapping annotation.

14. How to get Request or Session in the method?

Answer: Declare the request directly in the formal parameters of the method, and SpringMvc will automatically pass in the request object.

15. If I want to get the parameters passed in from the front desk in the interception method, how to get it?

Answer: You can declare this parameter directly in the formal parameter, but the name must be the same as the passed parameter.

16. If there are many parameters passed in at the front desk, and these parameters are all an object, how to quickly get this object?

Answer: Declare this object directly in the method, and SpringMvc will automatically assign properties to this object.

17. What is the return value of the function in SpringMvc?

Answer: The return value can have many types, including String, ModelAndView. The ModelAndView class combines the view and data together, but it is generally better to use String.

18. What object does SpringMvc use to transfer data from the background to the foreground?

Answer: Through the ModelMap object, you can call the put method in this object, add the object to it, and the front desk can get it through the el expression.

19. How to put the data in ModelMap into Session?

Answer: You can add @SessionAttributes annotation to the class, the string contained in it is the key to be put into the session.

 

20. How is the interceptor written in SpringMvc:

There are two ways of writing, 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 SpringMvc configuration file:

  <!-- Configure SpringMvc interceptor-->
 
<mvc:interceptors>
 
    <!-- Configure an interceptor Bean. The default is to intercept all requests-->
 
    <bean id="myInterceptor" class=" com.zwp.action.MyHandlerInterceptor"></bean>
 
    <!-- Only for partial request interception-->
 
    <mvc:interceptor>
 
       <mvc:mapping path="/modelMap.do" />
 
       <bean class="com .zwp.action.MyHandlerInterceptorAdapter" />
 
    </mvc:interceptor>
 
</mvc:interceptors>
 

21. Annotation principle:

Annotation is essentially a special interface that inherits Annotation, and its specific implementation class is a dynamic proxy class generated by Java runtime. When we get annotations through reflection, what we return is a dynamic proxy object generated by the Java runtime. The method of calling a custom annotation through the proxy object will eventually call the invoke method of AnnotationInvocationHandler. This method will index the corresponding value from the map of memberValues. The source of memberValues ​​is the Java constant pool.

Guess you like

Origin blog.csdn.net/qq_30764991/article/details/100936436