Spring MVC
1.Spring MVC继承体系
1.1.SpringMVC的概念
Spring Web MVC 是基于 Servlet API 构建的原始 Web 框架,从一开始就已包含在 Spring Framework 中。正式名称“ Spring Web MVC”来自其源模块的名称(spring-webmvc),但更通常称为“ Spring MVC”。
SpringMVC框架围绕DispatcherServlet这个核心展开,DispatcherServlet是SpringMVC框架的总导演、总策划,它负责截获请求并将其分派给相应的处理器处理
2.SpringMVC架构
SpringMVC运行流程:
3.springweb mvc程序(HelloWorld)
1 导入jar包
2 配置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">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--
/*:拦截所有请求,包括jsp
/ :拦截所有请求,不包含jsp
*.do,*.action
-->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
3 springmvc的配置文件
{servlet-name}-servlet.xml
用户发送请求到web容器,并被DispatchServlet拦截之后进入springmvc容器,springmvc该怎么处理那,这就需要springmvc的配置文件
在DispatcherServlet父类中的注释有说明配置文件 .xml 的命名方式:
由此知道,springmvc默认读取/WEB-INF/{servlet-name}-servlet.xml这个配置文件,因为我们在web.xml中的servlet-name配置的是springmvc,所以在WEB-INF目录下创建springmvc-servlet.xml({servlet-name}-servlet.xml为文件固定名称写法)文件
springmvc配置文件的头信息和spring一样
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
完整的配置
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置映射器,把bean的name属性作为一个url -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!-- 配置适配器 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<bean name="/hello.do" class="com.springmvc.HelloController"/>
<!-- 配置视图解析器 -->
<!-- Example: prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="test" -> "/WEB-INF/jsp/test.jsp" -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
由此可见,视图解析器的规则是:prefix+viewName+suffix
4 HelloController内容
/**
* @Author: Ron
* @Create: 2021 15:24
* 在整体架构中,通常称为Handler
* 在具体实现中,通常称为Controller
* spring mvc xml文件配置版
*/
public class HelloController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView mv = new ModelAndView();
mv.setViewName("hello"); // 试图名称
mv.addObject("msg", "这是我的第一个SpringMVC程序!!"); // 数据模型
return mv;
}
}
5 视图页面 .jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>title</title>
</head>
<body>
<div style="color:red; font-size:30px">${msg}</div>
</body>
</html>
6 运行测试
http://localhost:8080/springwebmvc/hello.do
4.流程分析
DispatcherServlet
5.注解版
controller层中的HelloController2类
/**
* @Author: Ron
* @Create: 2021 15:37
* spring mvc注解版
*/
@Controller
public class HelloController2 {
@RequestMapping("show1")
public ModelAndView test1() {
ModelAndView mv = new ModelAndView();
mv.setViewName("hello"); // 试图名称
mv.addObject("msg", "这是注解版的SpringMVC程序!!"); // 数据模型
return mv;
}
}
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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置映射器,把bean的name属性作为一个url -->
<!-- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> -->
<!-- 推荐使用的注解映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!-- 配置适配器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> -->
<!-- 推荐使用的注解适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!--利用注解可省略此处配置-->
<!-- <bean name="/hello.do" class="com.springmvc.controller.HelloController"/>-->
<!-- 配置注解扫描,和Spring的配置方式一样 -->
<context:component-scan base-package="com.springmvc.controller"/>
<!-- 配置视图解析器 -->
<!-- Example: prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="test" -> "/WEB-INF/jsp/test.jsp" -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
测试
http://localhost:8080/springwebmvc/show1.do
5.2.最佳方案(注解驱动)
注解配置最终方案
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注解驱动,替代推荐使用的注解映射器和适配器,并提供了对json的支持 -->
<mvc:annotation-driven/>
<!-- 配置注解扫描,和Spring的配置方式一样 -->
<context:component-scan base-package="com.springmvc.controller"/>
<!-- 配置视图解析器 -->
<!-- Example: prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="test" -> "/WEB-INF/jsp/test.jsp" -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
6.SpringMVC特有内置对象
6.1.controller层的HelloController2类
controller方法除了返回ModelAndView,还可以返回String。当返回值是String时,默认是视图名称。
那么数据模型怎么办?SpringMVC提供了特有的内置对象:Model ModelMap 本质都是Map
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Author: Ron
* @Create: 2021 10:20
*/
@Controller
public class HelloController3 {
@RequestMapping("show2")
public String test2(Model model) {
model.addAttribute("msg","SpringMVC特有内置对象,Model数据模型");
return "hello";
}
}
6.2.测试
http://localhost:8080/springwebmvc/show2.do
6.3.JSON
在实际开发过程中,json是最为常见的一种方式,所以springmvc提供了一种更为简便的方式传递数据。
@ResponseBody 是把Controller方法返回值转化为JSON,称为序列化
@RequestBody 是把接收到的JSON数据转化为Pojo对象,称为反序列化
原理
6.4.Json的转化是由注解驱动完成的。
注解驱动会判断是否引入了jackson依赖,并决定是否加载json转化的消息转化器
@ResponseBody
当一个处理请求的方法标记为@ResponseBody时,表示该方法需要输出其他视图(json、xml),springmvc通过默认的json转化器转化输出
controller
需要把什么转化为json,就返回什么数据
1.当方法上有@ResponseBody注解,代表方法的返回值需要输出其他视图
2.把方法的返回值转化为其他视图(json)