Six. For SpringMVC framework

6.1 What is SpringMVC? A little bit about your understanding of SpringMVC?

SpringMVC is a lightweight Web framework based on Java that implements the MVC design pattern of request driven type. 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

6.2 SpringMVC Process? (will be)

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

(2) After receiving the request, the DispatcherServlet invokes the ejector of HandlerMapping processor to request to obtain Handle;

(3) The processor mapper finds the specific processor according to the request URL, generates the processor object and the processor interceptor (if any) and returns them to the DispatcherServlet;

(4) DispatcherServlet calls HandlerAdapter processor adapter;

(5) The HandlerAdapter is adapted to call the specific processor (Handler, also called the back-end controller);

(6) The Handler returns ModelAndView after execution;

(7) The HandlerAdapter returns the Handler execution result ModelAndView to the DispatcherServlet

(8) DispatcherServlet sends ModelAndView to ViewResolver view parser for parsing;

(9) The ViewResolver returns the concrete View after parsing;

(10) DispatcherServlet renders the View (that is, the model data is filled into the View)

(11) DispatcherServlet responds to users.

6.3 What are the main components of SpringMVC?

(1) Front-end controller DispatcherServlet (no programmer development required)

Function: Receives requests. The response result, which is equivalent to a forwarder, with DispatcherServlet reduces coupling between other components.

(2) Processor mapping HandlerMapping (no programmer development required)

Finds the Handler based on the requested URL

(3) HandlerAdapter

Note: The Handler must be written according to the rules required by the HandlerAdapter so that the adapter HandlerAdapter can properly execute the Handler.

(4) Processor Handler (programmer development required)

(5) ViewResolver (no programmer development required)

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

(6) View View (need programmer to develop JSP)

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

6.4 What are the differences between SpringMVC and Struts2?

(1) for SpringMVC entry is a servlet that front controller (DispatchServlet), and struts 2 is a filter blocking entrance (StrutsPrepareAndExecuteFilter).

(2) SpringMVC is based on method development (a URL corresponds to a method), request parameters are passed to the method parameter, can be designed as singleton or multiple cases (suggested singleton), Struts2 is based on class development, transmission parameters are through the attributes of the class, can only be designed as multiple cases.

(3) Struts uses value stack to store the data of request and response, and accesses data through OGNL. SpringMVC parses the content of request through parameter parser, assigns values to method parameters, and encapsulates data and view into ModelAndView object. Finally, the schema data in the ModelAndView is transferred to the page via the RequES domain. The Jsp view parser uses JSTL by default.

6.5 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:

(1) Add jackson. jar

(2) Configure the JSON mapping in the configuration file

(3) You can return an Object, a List, etc., with the @responseBody annotation in front of the method

6.6 How to Solve the Problem of Chinese Garbled Characters in Post Requests and How to Handle Get Requests?

Configure a CharacterEncodingFilter 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> 
Copy the code

(2) Garbled characters appear in Chinese parameters of GET requests. There are two solutions: Modify the Tomcat configuration file and add the code 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”).get Bytes(“ISO8859-1″),” UTF-8 “) ISO8859-1 is the tomcat default encoding. You need to encode the Tomcat encoded content according to UTF-8.

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

6.8 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; We can then configure interceptors in the SpringMVC configuration file:

<! -- Configure SpringMVC interceptor --> <mvc:interceptors>  <! The default is to intercept all requests --> 

 
 
<bean id="myInterceptor" class="com.zwp.action.MyHandlerInterceptor"></bean> <! -- Only partial request interception --> <mvc:interceptor>  <mvc:mapping path="/modelMap.do" /> <bean class="com.zwp.action.MyHandlerInterceptorAdapter" />   </mvc:interceptor> </mvc:interceptors>
Copy the code

p.do” /> </mvc:interceptor> </mvc:interceptors>

Copy the code