MVC pattern:
The components in the program can be divided into model, View, controller three parts according to their responsibilities.
- Model models: Responsible for business processing and data encapsulation (e.g. DAO)
- View view: Responsible for interacting with user interface (e.g. JSP)
- Controller controllers: Receive requests, reconcile models and views, complete processing and corresponding (such as servlet)
Spring MVC mainly consists of components:
- Dispatcherservlet: Host controller, receiving requests, configuring in Web. xml
- Handlermapping: Find the controller according to the request
- Controller: Invoking a logic processing request such as DAO (Developer-written)
- Modelandview: Encapsulating model and view information
- Viewresolver: View parser, find the corresponding JSP page according to Modelandview, return the data
Spring MVC processing Process:
- The browser makes a request and requests to enter Dispatcherservlet
- Dispatcherservlet calls Handlermapping, looking for controller component processing on request
- The controller processes the request and returns a Modelandview object
- Viewresolver Parse Modelandview information, find the corresponding JSP page, return the data
Example:/hello.form-->dispatcherservlet-->handlermapping-->hellocontroller-->modelandview--> viewresolver-->hello.jsp
Spring MVC configuration:
First configure the Dispatcherservlet in Web. xml
<!--Spring MVC Core Controller Dispatcherservlet configuration - <servlet> <Servlet-name>Springmvc</Servlet-name> <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class> <Init-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>Classpath*:spring/spring-mvc.xml</Param-value> </Init-param> <!--whether to load this servlet at boot time - <!--A value of 0 or greater than 0 o'clock indicates that the container loads and initializes the servlet when the app starts, or when the value is less than 0 or unspecified, indicating that the container will not load until the servlet is selected - <!--The lower the value of a positive number, the higher the precedence of the servlet, and the more loaded it will be when the app starts. When the value is the same, the container will select its own order to load - <Load-on-startup>1</Load-on-startup> </servlet> <servlet-mapping> <Servlet-name>Springmvc</Servlet-name> <Url-pattern>/</Url-pattern> </servlet-mapping>
Then follow the classpath path above to create the configuration file Spring-mvc.xml:
<!--Note the package path for the default scan - <Context:component-scanBase-package= "Com.xzh.ssm.controller" /> <!--adding MVC annotation Drivers - <Mvc:annotation-driven/><!--add a prefix to a model view - <BeanID= "Viewresolver"class= "Org.springframework.web.servlet.view.InternalResourceViewResolver"P:prefix= "/web-inf"P:suffix= ". jsp" /><!--get static resources do not intercept - <mvc:resources Location="/"Mapping= "/**/*.html" /> <mvc:resources Location="/"Mapping= "/**/*.js" /> <mvc:resources Location="/"Mapping= "/**/*.css" /> <mvc:resources Location="/"Mapping= "/**/*.png" /> <mvc:resources Location="/"Mapping= "/**/*.gif" /><!--within MVC capture specified exceptions, redirect pages, other exceptions to config Error-page in Web. XML - <Beanclass= "Org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> < Propertyname= "Exceptionmappings"> <Props> <propKey= "Java.long.Exception">/jsp/error</prop> </Props> </ Property> </Bean>
Spring MVC Learning Notes