首页 > 代码库 > Spring MVC------->version4.3.6-------->第一个Spring MVC程序

Spring MVC------->version4.3.6-------->第一个Spring MVC程序

step1,配置DispatcherServlet

    • web.xml中配置DispatcherServlet,使其拦截相应的请求
      <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>

      如上配置,使得DispatcherServlet实例dispatcher可以拦截所有以.do结尾的请求

 

 

step2,编写DispatcherServlet对应的WebApplicationContext,也即[servlet-name]-servlet.xml.

 

    • step1中servlet-name设置为dispatcher,所以该DispatcherServlet实例对应的WebApplicationContext就是dispatcher-servlet.xml 
    • [servlet-name]-servlet.xml中管理哪些beans?

step3,编写其他ApplicationContext(即IOC容器),实际上也就是spring对应的各个xml形式的配置文件,里面定义了各种beans

    •  /WEB-INF/spring-services.xml里面定义了各种业务逻辑组件bean
    • 再如 /WEB-INF/spring-dao.xml里面定义了各种数据持久化组件bean  

step4,在web.xml中配置一个上下文载入器,使得step2中所说的对应于DispatcherServlet实例的WebApplicationContext能够被自动加载,得到拥有其定义的所有beans的IOC容器

    • web.xml 
      <servlet>
                 <servlet-name>context</servlet-name>
                 <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
                 <load-on-startup>2</load-on-startup>
      </servlet>

 

step4,还需要在web.xml中指定配置文件的位置,只有这样,step4中的上下文加载器才能知道该到哪里去加载相应的ApplicationContext实例

    • web.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>

阶段性小结:step1-step4所形成的的web.xml文件

    • <?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/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>
        
        <!-- 定义ApplicationContext文件的位置 -->
        <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>
        
        <!-- 定义DispatcherServlet对象,并且配置servlet-mapping,使得DispatcherServlet对象能够拦截相应的请求 -->
        <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>
         
         <!-- 在web.xml中指定一个上下文加载器,使得DispatcherServlet对应的WebApplicationContext
                     (也即[servlet-name]-servlet.xml)能够被自动加载
               也使得 普通类型的spring ApplicationContext能够被自动加载-->
         <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-------->第一个Spring MVC程序