1 什么是SpringMVC
是一個mvc框架,用來簡化基於mvc架構的web應用程序的 開發。
2 SpringMVC五大組件
DispatcherServlet (前端控制器)
HanlderMapping
Controller (處理器)
ModelAndView
ViewResolver (視圖解析器)
2.1 springMVC 的五大組件怎么協同工作
》DispatcherServlet收到請求之后,依據 HandlerMapping的配置調用相應的處理器來處理。
》處理器將處理結果封裝成ModelAndView, 然后返回給DispatcherServlet。
》DispatcherServlet依據ViewResovler的 解析,調用相應的視圖對象(比如某個jsp)來生成 相應的頁面。
3 SpringMVC編程步驟
3.1 導包 : spring-webmvc
》需要配置運行環境為 Tomcat
3.2 添加一個spring的配置文件

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" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" 5 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" 7 xmlns:jpa="http://www.springframework.org/schema/data/jpa" 8 xsi:schemaLocation=" 9 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 11 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd 12 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd 13 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 14 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 15 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 16 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 17 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> 18 19 <!-- 配置HandlerMapping --> 20 <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 21 <property name="mappings"> 22 <props> 23 <prop key="/hello.do">helloController</prop> 24 </props> 25 </property> 26 </bean> 27 <bean id="helloController" class="test.TestController"/> 28 29 <!-- 配置ViewResolver --> 30 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 31 <property name="prefix" value="/WEB-INF/"/> 32 <property name="suffix" value=".jsp"/> 33 </bean> 34 35 </beans>
3.3 配置DispatcherServlet

1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> 3 <display-name>springmvc01s</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 DispatcherServlet的初始化方法會啟動spring容器, 18 contextConfigLocation用於指定spring配置文件的位置 19 --> 20 <init-param> 21 <param-name>contextConfigLocation</param-name> 22 <param-value>classpath:spring-mvc.xml</param-value> 23 </init-param> 24 <load-on-startup>1</load-on-startup> 25 </servlet> 26 <servlet-mapping> 27 <servlet-name>springmvc</servlet-name> 28 <url-pattern>*.do</url-pattern> 29 </servlet-mapping> 30 </web-app>
3.4 寫Controller(處理器)
3.5 寫jsp
3.6 配置HandlerMapping,ViewResolver
4 SpringMVC編程步驟(利用注解實現)
step1. 導包。
step2. 添加一個spring的配置文件。

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" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" 5 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" 7 xmlns:jpa="http://www.springframework.org/schema/data/jpa" 8 xsi:schemaLocation=" 9 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 11 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd 12 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd 13 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 14 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 15 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 16 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 17 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> 18 <!-- 配置注解掃描 --> 19 <context:component-scan base-package="test"></context:component-scan> 20 <!-- 配置mvc注解掃描 --> 21 <mvc:annotation-driven></mvc:annotation-driven> 22 23 <!-- 配置ViewResolver --> 24 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 25 <property name="prefix" value="/WEB-INF/"/> 26 <property name="suffix" value=".jsp"/> 27 </bean> 28 29 </beans>
step3. 配置DispatcherServlet。
step4. 寫Controller(處理器)。
要求:
a. 不用實現Controller接口。
b. 可以添加多個方法。
c. 方法名不做要求,返回值可以是ModelAndView, 也可以是String。
d. 將@Controller添加到類名前。
e. 將@RequestMapping添加到類名前或者方法前。
step5.寫jsp。
step6.配置ViewResolver,組件掃描,mvc注解掃描。
注:只有配置了mvc注解掃描,@RequestMapping才起 作用
5 參數獲取、響應數據
待更新...2017年5月13日22:41:15
本博客源代碼2:點擊前往
本博客源代碼1:點擊前往