SpringMVC learning notes (1), SpringMVC learning notes (
1. MVC Flowchart
Analysis Flowchart
1. First, the user sends a request ----> front-end controller. The front-end Controller determines which page controller to process based on the request information (such as URL) and delegates the request to it, that is, the control logic of the previous Controller; Steps 1 and 2 in the figure;
2. After receiving the request, the page controller needs to collect and bind the request parameter to an object. This object is called a command object in Spring Web MVC and is verified, then, the command object is delegated to the Business Object for processing. After processing, a ModelAndView (model data and logical view name) is returned. Steps 3, 4, and 5 are shown in the figure;
3. The front-end controller revokes control. Then, based on the returned logical view name, it selects the corresponding view for rendering and passes the model data into the view for rendering. steps 6 and 7 in Figure 2;
4. The front-end controller revokes control again and returns the response to the user. Step 8 in the figure ends.
2. SpringMVC Flowchart
SpringWorkflow description
1. the user sends a request to the server, and the request is captured by the Spring front-end control Servelt DispatcherServlet;
2. DispatcherServlet parses the request URL to obtain the request Resource Identifier (URI ). Then, according to the URI, call HandlerMapping to obtain all the related objects (including the Handler object and the interceptor corresponding to the Handler object) of the Handler configuration, and return the result in the form of the HandlerExecutionChain object;
3. Based on the obtained Handler, DispatcherServlet selects a suitable HandlerAdapter. (Notes: If HandlerAdapter is obtained successfully, the preHandler (...) method of the interceptor will be executed)
4. Extract the model data in the Request, fill in the Handler input parameter, and start to execute Handler (Controller ). In the process of filling in the input parameters of Handler, Spring will help you with some additional work according to your Configuration:
HttpMessageConveter: converts a request message (such as Json and xml data) into an object and converts the object to the specified response information.
Data conversion: Convert the data of the request message. For example, convert String to Integer or Double.
Data root: format the request message. For example, convert a string to a formatted number or a formatted date.
Data Verification: verify the validity (length, format, etc.) of the data. The verification results are stored in BindingResult or Error.
5. After Handler is executed, return a ModelAndView object to DispatcherServlet;
6. Based on the returned ModelAndView, select a suitable ViewResolver (ViewResolver must be registered in the Spring container) and return it to DispatcherServlet;
7. ViewResolver combines Model and View to render the View
8. Return the rendering result to the client.
3. Create SpringMVC:
1. Engineering Structure
2. Add the jar package required by springMVC
3. Add SpringMVC configuration in the Web. xml configuration file
<Servlet> <servlet-name> springMVC </servlet-name> <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> <init-param> <param-name> contextConfigLocation </param-name> <! -- SpringMVC configuration file not in the case of WEB-INF classpath: ========>/web-inf/class/(the folder of the bytecode file generated after the file under the source file is compiled) <param-value> classpath: springMVCAnnotation. xml </param-value> --> </init-param> </servlet> <servlet-mapping> <servlet-name> springMVC </servlet-name> <url-pattern> *. do </url-pattern> </servlet-mapping>
4. Add a WEB-INF profile under the springMVC-servlet.xml
<! -- HandleMapping: ing function: locate the corresponding Controller class based on the request URL --> <bean class = "org. springframework. web. servlet. handler. beanNameUrlHandlerMapping "> </bean> <! -- Register the control layer class --> <bean name = "/index. do" class = "com. eduask. ykq. controller. BeanNameController"> </bean> <! -- View parser --> <bean class = "org. springframework. web. servlet. view. internalResourceViewResolver "> <property name =" prefix "value ="/view/"> </property> <property name =" suffix "value = ". jsp "> </property> </bean>
5. Create a Controller class
public class BeanNameController extends AbstractController { @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("==========="); ModelAndView andView=new ModelAndView("beanName"); return andView; }}