SpringMVC Factory Frequently Asked Technical Interview Questions Daquan——Gold Three Silver Four Enter Large Factory

6ed7d7ccbaec477ea885b397139fbbc2.png


content

⭐What is your understanding of the SpringMVC framework?

⭐SpringMVC main components?

⭐The execution process of SpringMVC and the role of each component?

⭐Writing of forwarding and redirection supported by SpringMVC?

⭐Common annotations of SpringMVC?

⭐ SpringMVC's idea and implementation of unified exception handling?

⭐In SpringMVC, if you want to pass data to the foreground by forwarding, how many ways to write it?

⭐What are the steps to use interceptors in SpringMVC?

⭐What are the steps for file uploading in SpringMVC? What are the three front-end elements?

⭐How to solve the problem of Chinese garbled characters in GET|POST requests in SpringMVC?

 


⭐What is your understanding of the SpringMVC framework?

SpringMVC is a request-driven lightweight web framework based on Java that implements the MVC design pattern . By separating Model, View, and Controller, it decouples the responsibilities of the web layer and divides complex web applications into logically clear parts. , simplify development, reduce errors, and facilitate the cooperation between developers in the group.


⭐SpringMVC main components?

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

Handler Mapper HandlerMapping : Find a Handler based on the requested URL

Handler Adapter HandlerAdapter : responsible for executing Handler

Handler : Java class that handles business logic

View resolver ViewResolver : parses the view, and parses ModelAndView into a real view according to the logical name of the view

View View : View is an interface, and its implementation class supports different view types, such as jsp, freemarker, pdf, etc.


⭐The execution process of SpringMVC and the role of each component? 

69eeda04506a4d81b93c36f1565d5bcb.png

1. The user sends a request to the front controller (DispatcherServlet)

2. The front controller (DispatcherServlet) receives the request and calls the handler mapper (HandlerMapping) to find the handler (Handler)

3. The Handler Mapping finds the specific handler (which can be searched according to the xml configuration and annotation), generates the handler object and the handler interceptor (if any) and returns it to the DispatcherServlet.

4. The front controller (DispatcherServlet) calls the handler mapper (HandlerMapping)

5. The handler adapter (HandlerAdapter) calls the custom handler class (Controller, also called the backend controller).

6. The custom processor class (Controller, also called back-end controller) processes the obtained parameters and returns the result to the handler mapper (HandlerMapping)

7. The handler adapter (HandlerAdapter) returns the result to the front controller (DispatcherServlet)

8. DispatcherServlet (front-end controller) passes ModelAndView to view resolver (ViewReslover)

9. The view resolver (ViewReslover) converts the obtained parameters from the logical view to the physical view and returns it to the front controller (DispatcherServlet)

10. The front controller (DispatcherServlet) calls the physical view to render and returns

11. The front controller (DispatcherServlet) returns the rendered result


⭐Writing of forwarding and redirection supported by SpringMVC?

Forward :

Forward method : add "forward:" before the return value, such as "forward:user.do?name=lisi"

Redirect :

Redirect method : add redirect : in front of the return value, such as "redirect: http://www.csdn.net"


⭐Common annotations of SpringMVC?

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

2. @RequestBody : The annotation implements receiving the json data of the http request and converts the json to a java object.

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

4. The @PathVariable user obtains the specified parameter from the url path, and marks it with @PathVariabel("name of the parameter to be obtained") before the parameter.

5. @RequestParam : Marked before method parameters, it is used to make some restrictions on incoming parameters. It supports three properties:

    - value: default attribute, used to specify the parameter name passed in by the front end

    - required: used to specify whether this parameter must be passed

    - defaultValue: Specify a default value when the parameter is a mandatory parameter and no parameter is passed in the front end

6. @ControllerAdvice is annotated on a class, indicating that the class is a global exception handling class.

7. @ExceptionHandler (Exception.class) is marked on the method in the exception handling class, indicating the type of exception that the method can handle.


⭐ SpringMVC's idea and implementation of unified exception handling?

After using SpringMVC, the caller of the code is the SpringMVC framework, which means that the final exception will be thrown into the framework, and then the framework will specify the exception handling class for unified processing

Method 1: Create a custom exception handler (implement the HandlerExceptionResolver interface), and implement the exception handling method inside, and then hand this class to the Spring container for management

Method 2: Annotate the class (@ControllerAdvice) to indicate that this is a global exception handling class, add an annotation (@ExceptionHandler) to the method, and have a value attribute in ExceptionHandler, which can specify the type of exception that can be handled


⭐In SpringMVC, if you want to pass data to the foreground by forwarding, how many ways to write it?

Method 1 : Directly use the request field to transfer data   

request.setAttirbuate("name", value);

Method 2 : Use Model to pass values, and the bottom layer will put the data into the request field for data transmission

model.addAttribuate("name", value);

Method 3 : Use ModelMap to pass values, and the bottom layer will put the data into the request field for data transmission

modelmap.put("name",value);

Method 4 : Borrowing ModelAndView to set data and views in it

mv.addObject("name",value);

mv.setView("success");

return mv;

⭐What are the steps to use interceptors in SpringMVC?

1. Define the interceptor class

SpringMVC provides us with the interface of the interceptor specification, create a class to implement HandlerInterceptor, and rewrite the abstract methods in the interface;

preHandle method: call this method before calling the processor. If the method returns true, the request will continue down, otherwise the request will not continue down, and the processor will not call

postHandle method: This method is called after the handler has been called

afterCompletion method: This method is called after the front controller has finished rendering the page

2. Register the interceptor

Register custom interceptor in SpringMVC core configuration file

<mvc:interceptors>

   <mvc:interceptor>

       <mvc:mapping path="拦截路径规则"/>

       <mvc:exclude-mapping path="不拦截路径规则"/>

       <bean class="自定义拦截器的类全限定名"/>

   </mvc:interceptor>

</mvc:interceptors>

⭐What are the steps for file uploading in SpringMVC? What are the three front-end elements?

File upload steps:

1. Add the commons-fileupload package required for file uploading

2. The configuration file upload parser, the id attribute of the file upload parser of the springmvc configuration file must be multipartResolver

3. The method parameter type of the receiving file corresponding to the backend must be MultipartFile, and the parameter name must be consistent with the name attribute of the front end


Three elements of the file upload front-end:

1. The form submission method must be post

2. The enctype attribute needs to be modified to: multipart/form-data

3. There must be an input tag with the type attribute of file, which needs to have a name attribute; if you need to upload multiple files, you need to add the multiple attribute


⭐How  to solve the problem of Chinese garbled characters in GET|POST requests in SpringMVC?

 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>
 

①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"/>

②Another method to recode 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 in utf-8.


If this article is helpful to my friends, I hope to give a like and support~ Thank you very much~

f4f7e55aa555460ab2a57056f43b9766.gif


 

Guess you like

Origin blog.csdn.net/weixin_42306958/article/details/122993596