1. MVC模式簡介
- M——Model模型:主要負責業務邏輯。包含兩層:業務數據和業務處理邏輯。比如實體類、DAO、Service都屬於模型層
- V——View視圖:負責顯示界面和用戶交互(收集用戶信息)。屬於視圖的組件是不包含業務邏輯和控制邏輯的JSP。
- C——Controller控制器:控制器是模型層和視圖層間的橋樑,用於控制流程。比如:在Servlet項目中的單一控制器ActionServlet
2. Spring Web MVC的核心組件
- DispatcherServlet(控制器,請求入口)
- HandlerMapping(控制器,請求派發)
- Controller(控制器,請求處理流程)
- ModelAndView(模型,封裝業務處理結果和視圖)
- ViewResolver(視圖,視圖顯示處理器)
3. 處理流程
- 瀏覽器向Spring發出請求,請求交給前端控制器DispatcherServlet處理(html頁面通過form表單提交action,到web.xml中配置前端控制器)
- 控制器通過HandleMapping找到相應的Controller組件處理請求(通過web.xml中的classpath:applicationContext.xml到applicationContext.xml中配置handlerMapping)
- 執行Controller組件約定方法處理請求,在約定方法調用模型組件完成業務處理。約定方法可以返回一個ModelAndView對象,封裝了處理結果數據和視圖名稱信息。
- 控制器接受ModelAndView之後,調用ViewResolver組件,定位View並傳遞數據信息,生成響應界面結果。
4. 一個簡單流程實例:
(1) 建立一個Dynamic Web project項目SpringMVC01
(2)建立login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登錄頁面</title>
</head>
<body>
<form action="login.do">
賬號:<input type="text"><br><br>
密碼:<input type="password"><br><br>
<input type="submit" value="登錄">
</form>
</body>
</html>
(3)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/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>SpringMVC01</display-name>
<!-- 1.前端控制器配置(DispatcherServlet) -->
<servlet>
<!-- 和下面servlet-mapping中的servlet-name一致 -->
<servlet-name>login</servlet-name>
<!-- 處理請求邏輯的類 (在jar包spring-webmvc中第一個包第二個類)-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化參數 ——加載applicationContext.xml配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name> <!-- 固定值,不能修改 -->
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<!-- 名字無要求 -->
<servlet-name>login</servlet-name>
<!-- 接受頁面請求路徑 -->
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
</web-app>
(4) applicationContext.xml
<?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:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 2. 配置handlerMapping(jar包在Libraries中的spring-webmvc第4個包倒數第三個類) -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<!-- property:通過setMappings()方法注入數據 -->
<property name="mappings">
<!-- 集合注入 -->
<props>
<prop key="/login.do">loginID</prop>
</props>
</property>
</bean>
<bean id="loginID" class="com.albb.controller.Login"></bean>
<!-- 4.配置ViewResolver (jar包在spring-webmvc中倒數第11個包倒數第6個類)-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".html"></property> <!-- 前綴和後綴兩者組合成/first.html -->
</bean>
</beans>
(5)Login.java
package com.albb.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
//3.配置controller組件 其實就是實現Controller接口
public class Login implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
System.out.println("ok");
return new ModelAndView("success");
}
}
(6) success.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>顯示頁面</title>
</head>
<body>
success!
</body>
</html>
(7) 運行login.html
由於只是測試,直接點登錄即可,能正確跳轉就行。