提前了解 三层架构
三层架构概述:
开发架构一般都是基于两种形式:
C/S架构:客户端/服务器
B/S架构:浏览器/服务器
在javaEE开发中,几乎都是基于B/S架构的开发。那么在B/S架构中,系统标准的三层架构包括:表现层、业务层、持久层。
表现层:也就是我们常说的web层。他负责接受客户端请求,向客户端响应结果,通常客户端使用http协议请求web层,web需要接受http请求,完成http响应。
业务层:也就是我们常说的service层。他负责业务逻辑处理。
持久层:dao层。负责数据持久化,和数据库做交互。
首先 需要了解什么是 MVC模式?
在经典MVC模式中,M是指模型,V是视图,C则是控制器,使用MVC的目的是将M和V的实现代码分离,从而使同一个程序可以使用不同的表现形式。其中,View的定义比较清晰,就是用户界面。
优点:分工明确,各司其职,互不干扰。适用于大型项目架构,有利于组件的重构
缺点:增加了系统开发的复杂度
SpringMVC原理:
SpringMVC是一种基于Java的实现MVC设计模型的请求驱动类型的轻量级Web框架。它通过一套注解,让一个简单的Java类成为处理请求的控制器,而无须实现任何接口。同时它还支持RESTful编程风格的请求。把复杂的web应用分成逻辑清晰的几部分。
简化开发,减少出错,方便组内开发人员之间的配合。
执行流程:
1.用户发送请求至 前端控制器DispatcherServlet。
2.前端控制器DispatcherServlet收到请求后调用处理器映射器HandlerMapping。
3.处理器映射器HandlerMapping根据请求的Url找到具体的处理器,生成处理器对象Handler及处理器拦截器HandlerIntercepter(如果有则生成)一并返回给前端控制器DispatcherServlet。
4.前端控制器DispatcherServlet通过处理器适配器HandlerAdapter调用处理器Controller。
5.执行处理器(Controller,也叫后端控制器)
6.处理器Controller执行完后返回ModelAndView。
7.处理器映射器HandlerAdapter将处理器Controller执行返回的结果ModelAndView返回给前端控制器DispatcherServlet。
8.前端控制器DispatcherServlet将ModelAndView传给视图解析器ViewResolver。
9.视图解析器ViewResolver解析后返回具体的视图View。
10.前端控制器DispatcherServlet对视图View进行渲染视图(即:将模型数据填充至视图中)
11.前端控制器DispatcherServlet响应用户。
从流程图中,我们可以看到:
接收前端传过来Request请求。
根据映射路径找到对应的处理器处理请求,处理完成之后返回ModelAndView。
进行视图解析,视图渲染,返回响应结果。
总结就是:参数接收,定义映射路径,页面跳转,返回响应结果。
当然这只是最基本的核心功能,除此之外还可以定义拦截器,全局异常处理,文件上传下载等等。
SpringMVC主要组件
前端控制器(DispatcherServlet):接收请求、响应结果,相当于转发器,有了DispatcherServlet就减少了其他组件之间的耦合度。
处理器映射器(HandlerMapping):根据请求的URL来查找Handler
处理器适配器(HandlerAdapter):负责执行Handler
处理器(Handler):处理业务逻辑的Java类
视图解析器(ViewResolver):进行视图的解析,根据视图逻辑名将ModelAndView解析成真正的视图。
视图(View):View是一个接口,他的实现类支持不同的视图类型,如jsp,freemarker等等。
案例
1.构建maven项目并添加依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.8</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
2.创建.jsp视图页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
首页
</body>
</html>
3.注解方式
(1)添加servlet依赖,并新建com.cqgcxy.config包
(2)在config包下创建配置类SpringMvcConfig替换spring-mvc.xml
package com.cqgcxy.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.cqgcxy.controller")
public class SpringMvcConfig {
}
(3)在config包下创建配置类ServletContainersInitConfig替换web.xml
package com.cqgcxy.config;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringMvcConfig.class);
return ctx;
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
protected WebApplicationContext createRootApplicationContext() {
return null;
}
}
4.在com.cqgcxy.controller包下创建HelloController控制器
package com.cqgcxy.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(){
System.out.println("hello···");
return "hello.jsp";
}
}
SpringMVC的请求响应流程
浏览器发送请求,被DispatcherServlet捕获,DispatcherServlet没有直接处理请求,而是将请求交给HandlerMapping处理器映射器,处理器映射器根据请求路径去Controller控制层中匹配对应的执行器,并将匹配结果返回给DispatcherServlet,由DispatcherServlet调用HandlerAdapter处理器适配器来执行控制层执行器方法;
执行器方法执行后的返回结果,由DispatcherServlet交给视图解析器ViewResolver来处理,找到对应的结果视图,渲染视图,并将结果响应给浏览器。
SpringMVC中的请求参数绑定
1、绑定的机制
表单中请求参数都是基于key=value的。SpringMVC绑定请求参数的过程是通过把表单提交请求参数,作为控制器中方法参数进行绑定的。
2、支持的数据类型
基本类型参数:包括基本类型和String
POJO类型参数:包括实体类,以及关联的实体类
数组和集合类型参数:包括List结构和Map结构的集合。
SpringMVC的数据响应方式
-
页面跳转
直接返回字符串
通过ModelAndView对象返回
-
回写数据
直接返回字符串
返回对象或集合
参数绑定示例
1、基本类型和string作为参数
(1)页面定义请求
<form action="/hello/test2" method="post">
用户名:<input name="userName" type="text">
年龄:<input name="age" type="text">
<input type="submit" value="提交">
</form>
(2)执行器方法绑定参数
@RequestMapping("test2")
public String test2(String userName,int age){
System.out.println("用户名:"+userName);
System.out.println("年龄:"+age);
return "index";
}
2、POJO类型作为参数
(1)页面定义请求
<form action="/hello/test3" >
用户名:<input name="pname" type="text">
年龄:<input name="age" type="text">
<input type="submit" value="提交">
</form>
(2)执行器方法绑定参数
@RequestMapping("test3")
public String test3(Person person){
System.out.println(person);
return "index";
}
自定义参数处理
SpringMVC不能自动识别参数转换为我们需要的数据类型,浏览器报400错误,类型转换异常。
public class MyDateConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date=simpleDateFormat.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}
配置
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<bean class="com.offcn.util.MyDateConverter"></bean>
</property>
</bean>
<mvc:annotation-driven conversion-service="conversionService"/>