1. 程式人生 > >Spring MVC五大核心元件及實現Spring MVC 的步驟

Spring MVC五大核心元件及實現Spring MVC 的步驟

1.Spring Web  MVC 五大核心元件 
  DispatcherServlet    控制器入口 負責分發請求 
  HandlerMapping       負責根據請求 找到對應的控制器
  Controller           真正處理請求的控制器 
  ModelAndView         封裝資料資訊和檢視資訊的 
  ViewResolver         檢視處理器 通過處理找到對應的頁面


2.實現Spring MVC 的步驟 
  2.1 建立一個 動態web 專案  在WEB-INF 下 建立一個 hello.jsp  
     匯入jar包(mvc  ioc)   拷貝Spring 配置檔案到 src 下 
  2.2 在web.xml 中 配置  DispatcherServlet  
    關聯Spring 的配置檔案 
  2.3 在Spring 的配置檔案中 配置HandlerMapping 介面對應的實現類 
  SimpleUrlHandlerMapping   指定請求路徑和 對應的處理控制器的
    對應關係
  2.4 寫一個 控制器類  實現 Controller 介面   完成返回 ModelAndView
     賦值hello.jsp 對應的檢視資訊
  2.5 在Spring 容器中建立 控制器物件  並關聯請求和控制器的物件關係   
  2.6 在Spring 配置檔案 配置 檢視處理器介面 ViewResolver 對應的

     實現類 InternalResourceViewResolver。

##配置檔案配置

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
xmlns:jee="http://www.springframework.org/schema/jee" 
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
   <!--  配置HandlerMapping  -->
   <bean  id="handlerMapping"   
      class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
      <!--  建立請求 和 控制器的對應關係  key 請求路徑  hello 是控制器id --> 
      <property name="mappings">
           <props>
                <prop key="/hello.do" >hello</prop>
           </props>
      </property>
   </bean>
   <!--  配置控制器物件  -->
   <bean  id="hello" class="com.xdl.controller.HelloController"></bean>
   <!--  配置檢視處理器  -->
   <bean  id="viewResolver" 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
      <property name="suffix"  value=".jsp"></property> 
   </bean>
</beans>