springMVC - springMVC basics

SpringMVC is a front-end control framework that will replace the traditional Servlet work. Spring MVC is a big component of Spring.


Disadvantages of Serclet :

    1- 1 servlet needs to configure at least 8 lines of xml . If there are many large-scale system servlets , the entire xml configuration will become very cumbersome.

    2-It is cumbersome for the servlet to obtain the data passed in by the user. Each time it receives a data, it needs to write  request.getParameter("name")

    3- The parameter types obtained by the server are all String types, and the programmer needs to manually convert the types.

    4- For an operation, you need to write a Servlet for processing

    5- The method of servlet is single, only doget () doPost ();

 

Components of SpringMVC:

    1- The front controller is only responsible for forwarding the request and response objects        

    2- The processor mapper matches the URL to the corresponding Controller    

    3- The processor adapter executes the corresponding handel     

    4- The view parser converts the logical name of the page into the corresponding specific page path          

 

Spring MVC calling process:

                                            

    1- The client sends a request request

    2- Request handler mapper to handle handler (find matching Controller class)

    3- Return the Controller class that can handle it

    4- The request processor adapter executes the handler (internally executed according to the matching rules)

    5-Execute handler (Controller-service--dao)

    6- Return the processing result and return the ModelAndView object (model represents the processed data View represents the logical name hello of the page)

    7- Return the ModelAndView object to the front controller

    8- Request the view parser to parse the View object (splicing the logical name of the page into a complete page path /WEB-INF/hello.jsp)

    9- Return the full page path to the front controller again

    10- Fill the model data into the Request field in the page


Getting Started

    1- Import the jar package:

                                             

    2- Configure Dis patcherServlet - in web.xml
                                        

    3- Write the core configuration file of spring MVC

        Name specification: By default, the name of the configuration file is + the name of the servlet + " -servlet.xml "  ----> (springmvc-servlet.xml)

                                    

    4- Implement the Controller interface

                                        

    5- Configure the view resolver

                            


springMVC modifies the location of the core configuration file:

  <servlet>
          <servlet-name>springmvc</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <!--Modify the location of the configuration file-->
          <init-param>
                  <param-name>contextConfigLocation</param-name>
                  <param-value>classpath:springmvc-servlet.xml</param-value>
          </init-param>
  </servlet>
  <servlet-mapping>
          <servlet-name>springmvc</servlet-name>
          <url-pattern>*.action</url-pattern>
  </servlet-mapping>

The annotation form of springMVC:

    1-Start springMVC annotation

                            

    2- Use the annotation @RequestMapping

                   

Get the request object and the response object:

                               

Solve the problem of Chinese garbled characters:

  <!--Solve the problem of Chinese garbled characters-->
  <filter>
          <filter-name>characterEncoding</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>characterEncoding</filter-name>
          <url-pattern>/*</url-pattern>
  </filter-mapping>


Guess you like

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