cgb2108-day16

一,SpringBoot

–1,概述

是由Spring团队开发的,整合了Spring框架,SpringMVC框架的所有jar包.
好处:
1.简化了Maven的操作,以前自己找jar包的坐标,现在直接创建springboot工程勾选你要的功能
2.SpringBoot项目可以快速启动/关闭,就是像服务器(Tomcat)一样的操作.被整合了
3.简单快速整合其他技术

–2,使用

1.创建SpringBoot项目–配置Maven
2.启动服务器,并且访问服务器里的资源
3,添加类

package cn.tedu.cgb2108boot01;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Hello {
    @RequestMapping("abc")
    public String get(){
        return "hello boot~";
    }
}

4,打开浏览器测试
http://localhost:8080/abc

二,SpringMVC框架

–1,概述

是Spring团队的产品,遵循MVC设计模式
MVC设计模式: 最终实现松耦合
M是Model是模型层,用来封装数据
V是View是视图层,用来展示数据
C是Controller是控制层, 接受浏览器发来的请求,并做出数据的响应
SpringMVC框架用来接受请求 + 做出响应

–2,工作原理

涉及五个组件:
1,前端控制器DispatcherServlet: 接受请求,并且调度
2,处理器映射器HandlerMapping: 根据地址栏的写法,找到能处理这次请求的类和方法
3,处理器适配器HandlerAdapter: 真正开始找到方法,执行方法体处理业务,并返回结果
4,视图解析器ViewResolver: 找到能够展示数据的页面
5,视图渲染View: 把数据展示在页面上

–3,解析请求参数

准备

浏览器发送数据给服务器有两种方式? get 和 post
get 的数据.在地址栏展示,用?拼接的参数
post的数据,安全不在地址栏展示
开发步骤: 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方式的请求参数

–1,测试

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值的返回给浏览器
    }
}


–2,总结

在这里插入图片描述

四,RestFul数据的解析

–1,测试

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;
    }

}

五,练习

–1,解析请求参数

制作一个前端HTML网页
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>浏览器向 服务器发起请求</title>
	</head>
	<body>
		<a href="http://localhost:8080/order/get?id=10&price=100&name=phone">练习1</a>
		<a href="http://localhost:8080/order/find/2">练习2</a>
		<a href="http://localhost:8080/order/save/10/100/phone">练习3</a>
	</body>
</html>

SpringMVC框架解析请求参数
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;

//MVC的C层Controller控制器,用来接受请求 给出响应
//总结:浏览器提交数据方式
//get:把数据用?进行拼接,多个数据之间用&连接(?id=10&price=100)
     //java程序解析请求参数:在方法的参数列表中,依次解析(或者封装成java对象)
//restful:可以简化get提交数据(/10/100)
    //java程序解析请求参数:使用{变量}来解析地址栏里的数据
        //+使用@PathVariable获取变量的值
@RestController
@RequestMapping("order")
public class OrderController {
//http://localhost:8080/order/get?id=10&price=100&name=phone
    @RequestMapping("get")
    public String get(Integer id,Double price,String name){
        return id+""+price+name ;
    }
    //http://localhost:8080/order/find
    @RequestMapping("find/{id}")
    public Integer find(@PathVariable Integer id){
        return id;
    }
    //http://localhost:8080/order/save/10/100/phone
    @RequestMapping("save/{id}/{price}/{name}")
    public String save(@PathVariable Integer id,
                       @PathVariable Double price,
                       @PathVariable String name){
        return id+price+name ;
    }
}

六,

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值