SpringMVC knowledge points summary

The MVC pattern

What does the MVC pattern do? In summary, there are mainly the following points:

① Map urls to Java classes (Struts2 framework) or Java methods (SpringMvc framework)

② Encapsulate the data submitted by users

③ Process the request – invoke the relevant business logic processing – encapsulate the data of the response

(4) Render the response data

For SpringMVC profile

SpringMVC is a lightweight, request-response based MVC framework. The main advantages are as follows:

①springMVC division of labor is very clear, can be said to be based on AOP implementation, the implementation of each function by a special object responsible for completion, including controller, validator, command object, model object, handler mapping, view parser and so on.

②springMVC supports internationalization.

③ Support multiple view technologies, such as JSP.

④ provide a simple and powerful JSP tag library, expenditure data binding function.

⑤ Provide built-in verifier, which can verify user input. If the verification is incorrect, the input form will be redirected.

Let’s do a simple comparison with Struts2 to give you a clearer picture of springMvc.

SpringMVC is based on method design, which is more granular. A method corresponds to a request context, and a method corresponds to a URL. Springmvc is very easy to implement as an architecture, whereas Struts2 is more difficult to implement because while an Action’s methods can correspond to a URL, the class’s properties are shared, making it impossible to annotate or otherwise indicate which method the properties belong to.

② From the data verification, springMvc support JSR3.0, processing Ajax requests is more convenient, only need a annotation @requestBody, and then the friend return response text request, and Struts2 is more troublesome.

③ In terms of configuration, Struts2 mostly uses traditional configuration files, while springMVC has almost no other configuration except springMVC-servlet.xml, so it is higher than Struts2 in terms of development efficiency.

(4) In terms of project management, SpringMVC and Spring are seamlessly integrated, which is not comparable to Struts2.

For starters, this might be a bit confusing to understand the comparisons and advantages, but come back later after the SpringMVC tutorial.

SpringMVC Workflow (Principles)

① The client sends requests to the DispatcherServlet

② The DispatcherServlet receives the request and invokes the HandlerMapping processor mapper

(3) Processor mapper finds the specific processor (this can be done based on XML configuration, annotations, generating processor objects and a processor interceptor (if any) back to the DispatcherServlet)

④ The DispatcherServlet then calls the HandlerAdapter processor adapter

⑤ The HandlerAdapter calls the specific processor Controller, also known as the back-end Controller, through the adapter.

⑥Controller Returns to MondelAndView.

The HandlerAdapter returns the Controler execution result ModelAndView to the DispatcherServlet.

The Dispatcherservlet passes the ModelAndView to the ViewReslover view parser

⑨ViewReslover returns the specific View after parsing.

⑩DischerServlet renders views based on views.

The final response is to the user.

Controller Configuration Summary

The oldest configuration method (URL to Bean)

1. Configuration web. XML

<! -- Define SpringMVC front-end controller DispatcherServlet--> <? The XML version = "1.0" encoding = "utf-8"? > <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "id =" WebApp_ID "version =" 3.0 "> <! <servlet> <servlet-name> SpringMVC </servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/springmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <! -- Intercepting Settings --> <servlet-mapping> <servlet-name> Springmvc </servlet-name> <! -- Block all requests by SpringMVC --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>Copy the code

2. Configuration for springmvc – config. XML

<! --springmvc-config.xml--> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <! - configuration handlerMapping - > < bean class = "org. Springframework. Web. Servlet. Handler. BeanNameUrlHandlerMapping" > < / bean > <! - handlerAdapter configuration - > < bean class = "org. Springframework. Web. Servlet. MVC. SimpleControllerHandlerAdapter" > < / bean > <! - to configure the renderer -- > < bean id = "jspViewResolver" class = "org. Springframework. Web. Servlet. The InternalResourceViewResolver" > <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> <! - configuration request and processor - > < bean name = ". / hello do "class =" com. Controller. HelloController "> < / bean > < / beans >Copy the code

3. Write the Controller class

package com.controller import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HelloController implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { ModelAndView modelAndView =new ModelAndView(); modelAndView.addObject("hello","hello"); modelAndView.setViewName("helloView"); return modelAndView; }}Copy the code

Annotation-based configuration (by far the most common)

1. Write web.xml

2. Configuration for springmvc – config. XML

<! --springmvc-config.xml--> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <! <context:component-scan base-package="com.controller" /> <! -- Enable Spring MVC annotations, Configure the annotation type of processor mapper -- > < bean class = "org. Springframework. Web. Servlet. MVC. The annotation. RequestMappingHandlerMapping" / > <! -- Enable Spring MVC annotations, Configure the annotation type of processor adapter - > < bean class = "org. Springframework. Web. Servlet. MVC. The annotation. RequestMappingHandlerAdapter" / > <! - the parser configuration view - > < bean class = "org. Springframework. Web. Servlet. The InternalResourceViewResolver" / > < / beans >Copy the code

3. Write the Controller class

package com.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class HelloController { @RequestMapping("/hello") public ModelAndView hello(){ ModelAndView modelAndView =new ModelAndView(); modelAndView.addObject("hello","hello"); modelAndView.setViewName("helloView"); return modelAndView; }}Copy the code

The last

I have arranged a: Spring family barrel series, Java systematic information, (including Java core knowledge, interview topics and the latest Internet real questions in 20 years, e-books, etc.) friends who need to pay attention to the public number can be obtained.