SpringMVC framework interview questions

Seven, SpringMVC framework

1. What is SpringMvc?

SpringMvc is a module of spring, a framework based on MVC, without the need for an intermediate integration layer to integrate.

2. How does SpringMVC work?

a. The client sends a request to DispatcherServlet
b. DispatcherServlet queries handlerMapping to find the Controller that handles the request c. After the Controller calls the business logic, it returns to ModelAndView
d. DispatcherServlet queries ModelAndView and finds the specified view e. The view returns the result to the client

3. SpringMVC process?

a. The user sends a request to the front-end controller DispatcherServlet.
b. The DispatcherServlet calls the HandlerMapping handler mapper upon receiving the request.
c. The processor mapper finds a specific processor (which can be searched according to the xml configuration and annotation), generates a processor object and a processor interceptor (if any) and returns it to DispatcherServlet.
d. DispatcherServlet calls HandlerAdapter handler adapter.
e. HandlerAdapter is adapted to call a specific processor (Controller, also called a back-end controller).
f. The Controller returns to ModelAndView after the execution is completed.
g. HandlerAdapter returns the controller execution result ModelAndView to DispatcherServlet. h, DispatcherServlet passes ModelAndView to ViewReslover view resolver.
i. ViewReslover returns the specific View after parsing.
g. DispatcherServlet renders the view according to the View (that is, fills the model data into the view).
k, DispatcherServlet responds to the user.

4. If you have also used struts2. Briefly introduce the difference between springMVC and struts2?

a. The entrance of springmvc is a servlet, that is, a front-end controller, while the entrance of struts2 is a filter.
b. Springmvc is based on method development (one url corresponds to one method), and the request parameters are passed to the formal parameters of the method, which can be designed as singletons or multiple instances (singletons are recommended),
struts2 is based on class development, and parameters are passed through classes Attributes can only be designed for multiple instances.
c. 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 sets the The model data in ModelAndView is transferred to the page through the requests field. The Jsp view resolver uses jstl by default.

5. What is the function of the @RequestMapping annotation on the class?

Is an annotation used to process the request address mapping, which can be used on a class or method. Used on a class, indicating that all methods in the class that respond to requests use this address as the parent path.

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

It is a singleton mode, so there are thread safety issues when accessing from multiple threads. Do not use synchronization, which will affect performance. The solution is to not write fields in the controller.

7. How to get Request, or Session in the method?

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

8. If there are many parameters passed in the foreground, and these parameters are all an object, how to get this object quickly?

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

9. What is the return value of a function in SpringMvc?

The return value can be of many types, such as String, ModelAndView. Generally, it is better to use String.

10. How does SpringMVC set redirection and forwarding?

Add "forward:" in front of the return value to forward the result, such as "forward:user.do?name=method4" Add "redirect:" in front of the return value to redirect the return value, such as "redirect:http: //www.baidu.com”

11. How does SpringMvc and AJAX call each other?

Through the Jackson framework, objects in Java can be directly converted into Json objects that can be recognized by Js.
The specific steps are as follows:
a. Add Jackson.jar
b. Configure the json mapping in the configuration file
c. In the accepting Ajax method, you can directly return Object, List, etc., but add @ResponseBody in front of the method

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

Solve the problem of garbled post request: configure a CharacterEncodingFilter filter in web.xml and set it to utf-8;

insert image description here

There are two solutions to the garbled characters in the Chinese parameters of the get request:
① Modify the tomcat configuration file to add the same encoding as the project encoding, 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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325088630&siteId=291194637