Detailed explanation of springmvc

SpringMVC part

1. Briefly talk about the workflow of SpringMVC?

Process 1. The user sends a request to the front-end controller DispatcherServlet . 2. The DispatcherServlet receives the request and calls the HandlerMapping handler mapper. 3. The processor mapper finds a specific processor, generates a processor object and a processor interceptor ( if any ) and returns it to the DispatcherServlet . 4. DispatcherServlet calls HandlerAdapter processor adapter 5. HandlerAdapter calls specific processor (Controller, also called back- end controller ) after adaptation . 6. After the controller is executed , return to ModelAndView . 7. HandlerAdapter will execute the result of the controller .






ModelAndView returns to DispatcherServlet 8 , DispatcherServlet passes ModelAndView to ViewReslover view parser 9 , ViewReslover returns to specific View after parsing 10 , DispatcherServlet renders view according to View (that is, fills model data into view). 11. DispatcherServlet responds to the user



 

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

Add in web.xml :

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

The above can solve the problem of garbled post request. There are two solutions for garbled characters in the Chinese parameters of the get request:

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 way 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 according to utf - 8

3. What is the main difference between SpringMVC and Struts2?

① The entrance of springmvc is a servlet , that is, a front-end controller, and the entrance of struts2 is a filter .

springmvc is based on method development , and parameters are passed through method parameters , which can be designed as a single instance or multiple instances ( single instance is recommended ) . Struts uses the value stack to store request and response data, and accesses data through OGNL .  Springmvc parses the content of the request object into method parameters through the parameter parser , encapsulates the response data and pages into ModelAndView objects, and finally converts the model data. Transferred to the page through the request object. The Jsp view resolver uses jstl by default .

 

Guess you like

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