概述
时Spring团队的产品,遵循MVC设计模式
MVC设计模式:
M是Model时模型层,用来封装数据
V是View,是视图层,用来展示数据
C是Controller是控制层,接受浏览器发来的请求,并做出数据的响应
SpringMVC框架用来接收请求+做出响应
解析请求参数
浏览器发送数据给服务器有两种方式:get和post
get的数据,在地址栏展示,用?拼接的参数
post的数据,不在地址栏展示,安全性较高
工作原理
涉及五个组件:
1.前段控制器 DispatcherServlet 接受请求,并且调度
2.处理器映射器 HandlerMapping:根据地址栏的写法,找到能处理这次请求的类和方法
3.处理器适配器HandlerAdapter:真正开始找到方法,执行方法体处理业务,并返回结果()
4.视图解析器ViewResolver:找到能够展示数据的页面
5.视图渲染View:把数据展示在页面上
开发步骤
1,导入SpringMVC相关的jar包(被Springboot整合了) 2, 使用注解开发
创建maven module
右键-new -Module-选择Maven-next-设置module name-finish
准备启动类
package cn.tedu.mvc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication//启动类,用来启动Module
public class RunApp {
public static void main(String[] args) {
//利用springboot启动当前类
SpringApplication.run(RunApp.class);
}
}
准备资源
package cn.tedu.mvc;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController//springmvc框架核心:接受请求
public class CarController {
//规定浏览器用什么规则访问资源
@RequestMapping("get")
public String get(){
return "欢迎~";
}
}
测试
http://localhost:8080/car/get
SpringMVC解析get方式的请求参数
测试
package cn.tedu.mvc;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController//springmvc框架核心:接受请求
@RequestMapping("car")//规定浏览器用什么规则访问资源
public class CarController {
//http://localhost:8080/car/get
@RequestMapping("get")//规定浏览器用什么规则访问资源
public String get(){
return "欢迎~";//直接把结果返回给浏览器展示
}
//携带着请求参数: http://localhost:8080/car/insert?id=666
//要求:如果方法有参数,调用时必须传入参数,否则500报错
//要求:参数列表里分为参数类型(参考地址栏里的参数的类型666)
// 参数名(参考地址栏里的参数名id)
@RequestMapping("insert")
public void insert(Integer id){
System.out.println(id);
}
//http://localhost:8080/car/save?id=666&price=9.9
@RequestMapping("save")
public String save(int id,String name,double price){
return id+name+price ;//给浏览器返回数据做展示
}
//http://localhost:8080/car/find?id=666&name=BMW&price=9.9&color=red
@RequestMapping("find")
//问题:想要解析很多请求参数太时,方法的参数列表太长...
//public String find(Integer id,String name,Double price,String color){
public Car find(Car a){
return a;//把a值的返回给浏览器
}
}
总结
RestFul数据的解析
测试
package cn.tedu.mvc;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("student")
public class StudentController {
//get方式:http://localhost:8080/student/save?id=666&name=jack&age=20
@RequestMapping("save")
public String save(Integer id,String name,Integer age){
return id+name+age;
}
//restful方式:http://localhost:8080/student/save2/666/jack/20
@RequestMapping("save2/{id}/{name}/{age}")
//{id}用来获取地址栏中的数据,并交给id变量保存--专门用来获取restful方式提交的数据
public String save2(@PathVariable Integer id,
//@PathVariable用来绑定地址栏里声明的变量的值,并交给同名变量保存
@PathVariable String name,
@PathVariable Integer age){
return id+name+age;
}
}
SpringMVC框架写post提交的数据
后端写法与get方式提交的数据没有什么不同
创建网页,提供表单
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>测试 表单提交数据</title>
<style>
/* 设置输入框的宽度高度 */
.a{
width:300px;
height: 30px;
}
/* 设置保存按钮 */
#btn1{
background-color: #0000FF;/* 背景色 */
border-color: #0000FF;/* 边框色 */
color: white;/* 文字颜色 */
width: 60px;/* 宽度高度 */
height: 30px;/* 宽度高度 */
}
/* 设置取消按钮 */
#btn2{
background-color: hotpink;/* 背景色 */
border-color: hotpink;/* 边框色 */
color: white;/* 文字颜色 */
width: 60px;/* 宽度高度 */
height: 30px;/* 宽度高度 */
}
</style>
</head>
<body>
<!--提交数据的要求:用form标签+有submit按钮+有name属性-->
<form method="post" action="http://localhost:8080/student/save">
<h1>学生信息管理系统MIS</h1>
<table>
<tr>
<td>姓名:</td>
</tr>
<tr>
<td>
<input class="a" type="text" placeholder="姓名..." name="user" />
</td>
</tr>
<tr>
<td>年龄:</td>
</tr>
<tr>
<td>
<input class="a" type="number" placeholder="年龄..." name="age" />
</td>
</tr>
<tr>
<td>
性别:(单选框)
<input type="radio" name="sex" value="1" checked="checked"/>男
<input type="radio" name="sex" value="0"/>女
</td>
</tr>
<tr>
<td>
爱好:(多选)
<input type="checkbox" name="hobby" value="ppq" checked="checked"/>乒乓球
<input type="checkbox" name="hobby" value="ps"/>爬山
<input type="checkbox" name="hobby" value="cg"/>唱歌
</td>
</tr>
<tr>
<td>
学历:(下拉框)
<select name="edu">
<option value="0">小学</option>
<option value="1">初中</option>
<option value="2">高中</option>
<option value="3">本科</option>
<option value="4">博士</option>
</select>
</td>
</tr>
<tr>
<td>入学日期:</td>
</tr>
<tr>
<td>
<input type="date" name="intime"/>
</td>
</tr>
<tr>
<td>
<button type="submit" id="btn1">保存</button>
<button type="reset" id="btn2">取消</button>
</td>
</tr>
</table>
</form>
</body>
</html>
创建Maven Module
创建启动类
package cn.tedu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RunApp {
public static void main(String[] args) {
SpringApplication.run(RunApp.class);
}
}
创建StudentController解析请求参数
package cn.tedu.controller;
import cn.tedu.pojo.Student;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("student")
public class StudentController {
//http://localhost:8080/student/save
@RequestMapping("save")
public String save(Student s){
return "访问成功!"+s;
}
}
创建Student类
package cn.tedu.pojo;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Arrays;
import java.util.Date;
//提供的属性用来: 封装 浏览器发来的数据
//要求:
// 1,变量名 必须和 网页中name属性的值 一样
// 2,变量类型 必须和 浏览器提交的数据类型 一样
public class Student {
//?user=jack&age=20&sex=1
private String user;
private Integer age;
private Integer sex;
private String[] hobby;
//用来保存,浏览器提交来的多个爱好ppq ps cg
private Integer edu;
//问题:原因是浏览器上选的日期是String类型:2021/10/17
//无法把String类型的日期变成 java.util.Date类型,报错400!!!!
// @DateTimeFormat用来转换日期的格式.y表示年M表示月d表示日
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date intime;
//get set toString
@Override
public String toString() {
return "Student{" +
"user='" + user + '\'' +
", age=" + age +
", sex=" + sex +
", hobby=" + Arrays.toString(hobby) +
", edu=" + edu +
", intime=" + intime +
'}';
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public Integer getEdu() {
return edu;
}
public void setEdu(Integer edu) {
this.edu = edu;
}
public Date getIntime() {
return intime;
}
public void setIntime(Date intime) {
this.intime = intime;
}
}
总结
拓展
package cn.tedu.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
//提供丰富的方法,方便的jdbc操作
public class JDBCUtils {
//1,获取数据库的连接(注册驱动+获取连接)
/**
* 获取数据库的连接
* @return 数据库的连接对象Connection
* @throws Exception
*/
static public Connection getConnection() throws Exception{
//1,注册驱动
Class.forName("com.mysql.jdbc.Driver");//全路径
//2,获取数据库的连接(用户名/密码)
//jdbc连接mysql数据库的协议//本机:端口号/数据库的名字 解决中文乱码 指定时区 关闭权限检验
String url="jdbc:mysql://localhost:3306/cgb2108?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false" ;
Connection c = DriverManager.getConnection(
url,"root","root");
return c ;//返回给调用者
}
}