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
由于只是测试,直接点登录即可,能正确跳转就行。