Step1, configuring Dispatcherservlet
-
- Configure Dispatcherservlet in Web. XML to intercept the corresponding request
<servlet> <Servlet-name>Dispatcher</Servlet-name> <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class> <Init-param> <Param-name>Contextconfiglocation</Param-name> <Param-value></Param-value> </Init-param> <Load-on-startup>1</Load-on-startup> </servlet> <servlet-mapping> <Servlet-name>Dispatcher</Servlet-name> <Url-pattern>*.do</Url-pattern> </servlet-mapping>
As configured above, so that Dispatcherservlet instance dispatcher can intercept all requests that end with. Do
Step2, write Dispatcherservlet corresponding webapplicationcontext, also namely [Servlet-name]-servlet.xml.
-
- Servlet-name is set to dispatcher in Step1, so webapplicationcontext for the Dispatcherservlet instance corresponds to Dispatcher-servlet.xml
- [What beans are managed in servlet-name]-servlet.xml?
Step3, writing other ApplicationContext (that is, the IOC container), which is actually the respective XML configuration file for spring, defines a variety of beans
-
- Various business logic component beans are defined as/web-inf/spring-services.xml
- Again , like/web-inf/spring-dao.xml, a variety of data persistence component beans are defined
STEP4, a context loader is configured in Web. XML, so that the webapplicationcontext that corresponds to Dispatcherservlet instances in Step2 can be loaded automatically, getting all the beans IOC containers that have their definitions
STEP4, you also need to specify the location of the configuration file in Web. XML, so that the context loader in Step4 will know where to load the corresponding ApplicationContext instance
-
- Xml
<Context-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>/web-inf/root-context.xml,/web-inf/dispatcher-servlet.xml,
/web-inf/spring-services.xml,
/web-inf/spring-dao.xml</Param-value> </Context-param>
Periodic summary: the Web. xml file formed by STEP1-STEP4
-
<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"Xmlns:javaee= "Http://java.sun.com/xml/ns/javaee"Xmlns:web= "Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"version= "2.4"> <Display-name>Archetype Created Web Application</Display-name> <!--define the location of the ApplicationContext file - <Context-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>/web-inf/root-context.xml,/web-inf/dispatcher-servlet.xml,/web-inf/spring-service S.xml,/web-inf/spring-dao.xml</Param-value> </Context-param> <!--defines the Dispatcherservlet object and configures the servlet-mapping so that the Dispatcherservlet object can intercept the corresponding request - <servlet> <Servlet-name>Dispatcher</Servlet-name> <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class> <!--<init-param> <param-name>contextConfigLocation</param-name> <param -value>/web-inf/dispatcher-servlet.xml </param-value> </init-param> - <Load-on-startup>1</Load-on-startup> </servlet> <servlet-mapping> <Servlet-name>Dispatcher</Servlet-name> <Url-pattern>*.do</Url-pattern> </servlet-mapping> <!--Specify a context loader in Web. XML, so that Dispatcherservlet corresponding Webapplicationcontext (also known as [Servlet-name]-servlet.xml) can be loaded automatically Also allows the normal type of spring ApplicationContext to be loaded automatically - <servlet> <Servlet-name>Context</Servlet-name> <Servlet-class>Org.springframework.web.context.ContextLoaderServlet</Servlet-class> <Load-on-startup>2</Load-on-startup> </servlet> </Web-app>
Spring MVC------->version4.3.6--------> First Spring MVC Program