码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-03-14 00:15:13      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:roo   数据持久化   arc   tar   archetype   javaee   并且   tco   web app   

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程序

标签:roo   数据持久化   arc   tar   archetype   javaee   并且   tco   web app   

原文地址:http://www.cnblogs.com/lxrm/p/6545583.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!