Interview questions-9SpringMVC

SpringMvc process

The user sends a request to the front controller DisPatcherServlet DisPatcherServlet
receives the request and calls the handlerMapping processor mapper The
processor mapper finds the specific processor, generates the processor object and the processor interceptor (if it is available), returns it to the dispatcherServlet
DispatcherServlet calls the HandlerAdapter for processing Adapter, the
adapter calls a specific processor handler to execute the ModelAndView after adaptation and returns it to the front controller. The
front controller passes the ModelAndView to the view parser for parsing. The front controller renders the returned view after parsing, and then Respond to the user

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 the cooperation between developers in the group.

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 the mapping strategy of various requested resources.

4. What are the main components of Spring MVC?

(1) The front controller DispatcherServlet (does not require programmers to develop)
Function: Receive requests and response results, which is equivalent to a repeater. With DispatcherServlet, the coupling between other components is reduced.
(2) Handler Mapping (no need for programmer development) Function: Find Handler according to the requested URL
(3) Handler adapter HandlerAdapter
Note: When writing Handler, it should be written in accordance with the rules required by HandlerAdapter, so that the adapter HandlerAdapter Only then can the Handler be executed correctly.
(4) Handler (programmer development is required)
(5) View resolver ViewResolver (programmer development is not required)
Function: to analyze the view and resolve it into a real view according to the logical name of the view
(6) View View (Need programmer to develop jsp) View is an interface, and
its implementation class supports different view types (jsp, freemarker, pdf, etc.)

6. How does SpringMVC set redirection and forwarding?

(1) Forwarding: add "forward:" before the return value, such as "forward:user.do?name=method4"
(2) Redirect: add "redirect:" before the return value, such as "redirect:http:/ /www.baidu.com"

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;
(2) There are two solutions for garbled Chinese parameters in get request: ①Modify the tomcat configuration file to add encoding and engineering The coding is the same, ②Another method is to re-code the parameters:

11. What are the common annotations 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.

.
21, notes 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/zyf_fly66/article/details/114070631