- springMVC概述
Spring MVC属于SpringFrameWork的后续产品;Spring 框架提供了构建 Web 应用程序的全功能MVC 模块;使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的SpringMVC框架或集成其他MVC开发框架。
- SpringMVC处理流程
- SpringMVC入门程序
- 创建web工程
- 导入springMVC相关jar包,在spring的基础上添加一个mvc的包
- 添加配置文件springmvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>
- 在web.xml中配置前端控制器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 配置SpringMVC前端控制器 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定SpringMVC配置文件 -->
<!-- SpringMVC的配置文件的默认路径是/WEB-INF/${servlet-name}-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<!-- 设置所有以action结尾的请求进入SpringMVC -->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
springMVC中url-patten取值:
- /*:拦截所有 jsp js png .css 真的全拦截.不建议使用
- *.action *.do:拦截以do action 结尾的请求
- /:拦截所有,不包括jsp,包含.js .png.css 建议使用
- 创建控制器MyFirstController.java
package demo1.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class MyFirstController {
@RequestMapping("/first.action")
public ModelAndView show(){
//创建ModelAndView用来存放数据和视图
ModelAndView modelAndView = new ModelAndView();
//设置数据到视图中
modelAndView.addObject("name","MymodelAndView");
//设置视图
modelAndView.setViewName("/first.jsp");
//将modelAndView返回
return modelAndView;
}
}
- 在springMVC核心配置文件springmvc.xml中添加控制器扫描范围
- 创建first.jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>First.jsp</title>
</head>
<body>
转发过来的数据name=${name}
</body>
</html>
- 在index.jsp中配置跳转
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>index</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/first.action">跳转到first.jsp</a>
</body>
</html>
- 将web工程部署到Tomcat中
- 运行Tomcat,结果如下
点击跳转链接后,可以看到地址栏的请求为.action结尾,被前端控制器DispatherServlet拦截处理后跳转到first.jsp页面,并显示出在Controller中设置的值
- SpringMVC详细执行流程
用户发送请求至前端控制器DispatcherServlet
DispatcherServlet收到请求调用HandlerMapping处理器映射器。
处理器映射器根据请求url找到具体的处理器,生成处理器对象及处理器拦截器(如果有则生成)一并返回给DispatcherServlet。
DispatcherServlet通过HandlerAdapter处理器适配器调用处理器
执行处理器(Controller,也叫后端控制器)。
Controller执行完成返回ModelAndView
HandlerAdapter将controller执行结果ModelAndView返回给DispatcherServlet
DispatcherServlet将ModelAndView传给ViewReslover视图解析器
ViewReslover解析后返回具体View
DispatcherServlet对View进行渲染视图(即将模型数据填充至视图中)。
DispatcherServlet响应用户
详细执行流程图
五、组件说明
1、前端控制器DispatcherServlet
用户请求到达前端控制器,它就相当于mvc模式中的C
dispatcherServlet是整个流程控制的中心
由它调用其它组件处理用户的请求
dispatcherServlet的存在降低了组件之间的耦合性
2、HandlerMapping 处理器映射器
HandlerMapping负责根据用户请求url找到Handler处理器
springmvc提供了不同的映射器实现不同的映射方式
HandlerMapping会把找到映射返回给前端控制器
3、Handler 后端控制器
在DispatcherServlet的控制下Handler对具体的用户请求进行处理。
4、HandlAdapter 处理器适配器
通过HandlerAdapter对处理器进行执行
5、ViewResolver 视图解析器
View Resolver负责将处理结果生成View视图
6、View
springmvc框架提供了很多的View视图类型的支持,
包括:jstlView、freemarkerView、pdfView等。
我们最常用的视图就是jsp。