一,五大核心組件 1.DispatcherServlet 請求入口 2.HandlerMapping 請求派發,負責請求和控制器建立一一對應的關係 3.Controller 處理器 4.ModelAndView 封裝模型信息和視圖信息 5.ViewResolver 視圖處理器,定位頁面 二,Spri ...
一,五大核心組件
1.DispatcherServlet 請求入口
2.HandlerMapping 請求派發,負責請求和控制器建立一一對應的關係
3.Controller 處理器
4.ModelAndView 封裝模型信息和視圖信息
5.ViewResolver 視圖處理器,定位頁面
二,Spring MVC的編寫步驟(訪問WEB-INF下的.jsp)
1.建立項目,導入jar包(ioc mvc)並且拷貝Spring容器中對應的配置文件到src下,並且在WEB-INF下創建一個hello.jsp
2.在web.xml中配置DispatcherServlet並通過初始化參數contextConfigLocation指定Spring容器對應的配置文件
3.在Spring配置文件中配置HandlerMapping的實現類SimpleUrlHandlerMapping
4.寫一個控制器類實現Controller介面,控制器方法中返回ModelAndView,在Spring容器中配置控制器
5.配置ViewResolver的實現類internalResourceViewResolver
如圖:
配置DispatcherServlet
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> 3 <display-name>spring-mvc</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 <!-- 配置請求入口 --> 13 <servlet> 14 <servlet-name>SpringMVC</servlet-name> 15 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 16 <!-- 配置初始化參數 --> 17 <init-param> 18 <param-name>contextConfigLocation</param-name> 19 <param-value>classpath:applicationContext.xml</param-value> 20 </init-param> 21 </servlet> 22 <servlet-mapping> 23 <servlet-name>SpringMVC</servlet-name> 24 <url-pattern>*.do</url-pattern> 25 </servlet-mapping> 26 </web-app>View Code
配置HandlerMapping
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:jdbc="http://www.springframework.org/schema/jdbc" 6 xmlns:jee="http://www.springframework.org/schema/jee" 7 xmlns:tx="http://www.springframework.org/schema/tx" 8 xmlns:aop="http://www.springframework.org/schema/aop" 9 xmlns:mvc="http://www.springframework.org/schema/mvc" 10 xmlns:util="http://www.springframework.org/schema/util" 11 xmlns:jpa="http://www.springframework.org/schema/data/jpa" 12 xsi:schemaLocation=" 13 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 14 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd 15 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd 16 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd 17 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd 18 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 19 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 20 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 21 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> 22 <!-- 配置請求分發器,讓請求和控制器之間建立一一對應關係 --> 23 <bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 24 <property name="mappings"> 25 <props> 26 <prop key="/toHello.do">helloController</prop> 27 </props> 28 </property> 29 </bean> 30 <!-- 配置控制器 --> 31 <bean id="helloController" class="com.xcz.controller.ToHelloController"></bean> 32 </beans>View Code
配置ViewResolver
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:jdbc="http://www.springframework.org/schema/jdbc" 6 xmlns:jee="http://www.springframework.org/schema/jee" 7 xmlns:tx="http://www.springframework.org/schema/tx" 8 xmlns:aop="http://www.springframework.org/schema/aop" 9 xmlns:mvc="http://www.springframework.org/schema/mvc" 10 xmlns:util="http://www.springframework.org/schema/util" 11 xmlns:jpa="http://www.springframework.org/schema/data/jpa" 12 xsi:schemaLocation=" 13 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 14 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd 15 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd 16 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd 17 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd 18 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 19 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 20 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 21 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> 22 <!-- 配置視圖處理器 --> 23 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 24 <property name="prefix" value="/WEB-INF/"></property> 25 <property name="suffix" value=".jsp"></property> 26 </bean> 27 </beans>View Code
最終配置結果
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:jdbc="http://www.springframework.org/schema/jdbc" 6 xmlns:jee="http://www.springframework.org/schema/jee" 7 xmlns:tx="http://www.springframework.org/schema/tx" 8 xmlns:aop="http://www.springframework.org/schema/aop" 9 xmlns:mvc="http://www.springframework.org/schema/mvc" 10 xmlns:util="http://www.springframework.org/schema/util" 11 xmlns:jpa="http://www.springframework.org/schema/data/jpa" 12 xsi:schemaLocation=" 13 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 14 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd 15 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd 16 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd 17 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd 18 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 19 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 20 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 21 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> 22 <!-- 配置請求分發器,讓請求和控制器之間建立一一對應關係 --> 23 <bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 24 <property name="mappings"> 25 <props> 26 <prop key="/toHello.do">helloController</prop> 27 </props> 28 </property> 29 </bean> 30 <!-- 配置控制器 --> 31 <bean id="helloController" class="com.xcz.controller.ToHelloController"></bean> 32 <!-- 配置視圖處理器 --> 33 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 34 <property name="prefix" value="/WEB-INF/"></property> 35 <property name="suffix" value=".jsp"></property> 36 </bean> 37 </beans>View Code
最後開啟服務,在瀏覽器上輸入localhost:埠號/項目名/toHello.do,看到如下界面,說明配置成功