Spring_MVC架构请求处理流程和使用

Spring MVC架构请求处理流程

  1. 用户发送请求☞DispatcherServlet
    DispatcherServlet是前端控制器,作为统一访问点,进行全局的流程控制。其收到请求后不直接进行处理,而是委托其他解析器进行处理。
  2. DispatcherServlet☞HandlerMapping
    HandlerMapping将请求映射为HandlerExecutionChain对象(一个Handler对象(页面控制器)和多个HandlerInterceptor对象(拦截器)),采用Strategy模式,很容易添加新的Strategy对象。
  3. DispatcherServlet☞HandlerAdapter
    HandlerAdapter将Handler包装为Adapter,采用Adapter模式,从而很容易支持多类型Handler
  4. HandlerAdapter☞Handler相应方法调用
    HandlerAdapter会根据适配结果调用真正地Handler处理方法,并返回一个ModelAndView对象(模型数据、逻辑视图名)
  5. ModeAndView中逻辑视图名☞ViewResolver
    ViewResolver将逻辑视图名解析为具体的View,采用策略模式,很容易更换其他视图技术
  6. Model☞View☞渲染
    View根据Model数据进行渲染,Model本质是一个Map数据结构,因此很容易支持各种视图技术
  7. DispatcherServlet☞响应客户端

Spring MVC框架的使用

  • pom.xml中添加依赖
    • spring web MVC 依赖
    • jackson 依赖
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.12.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.10.0</version>
</dependency>
  • 配置web.xml文件
<!-- 配置root容器的配置文件路径 -->
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:root.xml</param-value>
</context-param>
<!--1. 第一个容器 监听器 监听ServletContext对象的创建和销毁的
	用来初始化一个叫Root的ioc容器 类型 WebApplicationContext
	root容器它是servlet的ioc容器的父容器 
	子容器可以使用父容器里面的对象
	但是父容器是不能使用子容器中的对象的
 -->
<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
 
 <!--
 	2. 第二个容器 servlet容器  
 	负责创建web层相关的对象
 	(controller、异常处理的、视图解析的、HandlerMapping等等)
  -->
<servlet>
	<servlet-name>springDispatcherServlet</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<!-- 配置servlet中的初始化的参数
		contextConfigLocation 参数对应的路径是一个spring ioc的配置文件
		在创建servlet对象时 会读取指定的配置文件创建WebApplicationContext对象 该对象管理的就是一个ioc容器
	 -->
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-servlet.xml</param-value>
	</init-param>
	<!-- web容器启动时 创建servlet对象 -->
	<load-on-startup>1</load-on-startup>
</servlet>

<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
	<servlet-name>springDispatcherServlet</servlet-name>
	<!-- / 表示所有的请求都被 前端控制器接收 -->
	<url-pattern>/</url-pattern>
</servlet-mapping>
  • 创建root.xml&spring-servlet.xml
    • root.xml可以暂时不用写东西
    • spring-servlet.xml中设置注解驱动和扫描包路径
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="com.test.controller"></context:component-scan>
  • 定义Controller类
@Controller
public class UserController {

	// 1. 定义一个普通方法 方法的参数用于接收客户端请求过来的参数 默认请求参数的名字和方法的参数的名字相同
	// 相应 使用ModelAndView 既包含数据又包含视图路径

	@RequestMapping("/login") // 配置方法的请求路径
	public ModelAndView login(String username, String password) {
		ModelAndView mav = new ModelAndView("/user.jsp");
		mav.addObject("username", username);
		return mav;
	}

	//2 . 返回值直接写String 把视图路径返回 模型数据保存在Model的参数中 
	// 使用session/request/application对象 参数里添加即可
	@RequestMapping("/login")
	public String login(String username, String password, Model model, HttpSession session) {
		model.addAttribute("username", username);
		session.setAttribute("username", username);
		return "/user.jsp";
	}

	// 3. 使用对象接收客户端参数  
	@RequestMapping("/loginUser")
	public String login(User user, Model model) {
		model.addAttribute("user", user);
		return "/user.jsp";
	}

	// 4. 使用map接收客户端参数
	@RequestMapping("/loginMap")
	public String login(@RequestParam Map<String, Object> user, Model model) {
		model.addAttribute("user", user);
		return "/user.jsp";
	}

	// 5. @RequestParam 添加在方法的参数前 用来约束请求参数的
	// name属性 指定客户端发送的参数的名字 例如值为abc时,程序会将客户端发送来的名字叫做abc的参数的值保存在指定的参数中
	// defaultValue 默认值属性 客户端不传值时 默认为指定的值
	// 默认是必须参数 要求客户端一定要传入该参数
	// 基本数据类型声明的参数,客户端必须要 传值的
	// http://localhost:8080/spring-mvc/testParam?pageNum=2&abc=zhangsan
	@RequestMapping("/testParam")
	public String testParam(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(name = "abc") String name) {
		return "/user.jsp";
	}

	// 6. 设置处理的请求方法 
	// @RequestMapping 添加method属性
	@RequestMapping(value = "/testMethod", method = { RequestMethod.POST })
	public String testMethod() {
		return "/user.jsp";
	}

	// 7.1 响应跳转方式
	@RequestMapping("/testResp")
	public String testResp() {
		//		return "/user.jsp"; // 服务端跳转
		return "redirect:/user.jsp"; // 客户端跳转
	}

	// 7.2 直接响应内容  有中文时需要设置响应头 设置编码
	@RequestMapping(value = "/testRespText", produces = { "text/plain;charset=utf-8" })
	@ResponseBody
	public String testRespText() {
		return "你好, world";
	}

	// 7.3 直接响应对象 会转换成json字符串返回  (导入jackson的依赖,ioc配置文件中添加注解驱动)
	@RequestMapping(value = "/testRespJson", produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public List<User> select() {
		List<User> users = new ArrayList<User>();
		users.add(new User(1, "qq", "34324"));
		users.add(new User(1, "qq", "34324"));
		users.add(new User(1, "qq", "34324"));
		users.add(new User(1, "qq", "34324"));
		return users;
	}
}

发布了340 篇原创文章 · 获赞 23 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/Chill_Lyn/article/details/103795028