亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

springboot與springmvc基礎入門講解

 更新時間:2021年07月12日 17:31:17   作者:cgblpx  
本篇文章主要介紹了詳解快速搭建Spring Boot+Spring MVC,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

一,SpringBoot

–1,概述

用來整合maven項目,可以和Spring框架無縫銜接。

–2,用法

–1,創(chuàng)建SpringBoot工程:File-New-Project-選擇Spring Init…-next-輸入groupId、項目id、選成jdk8-next-選擇SpringWeb-ok

–2,配置Maven:File-Settings-選擇Build…-Maven-修改三處(解壓的位置、settings.xml位置-本地倉庫位置)-ok

–3,找到自動生成的一個類,直接運行 ( 啟動服務器 )

在這里插入圖片描述

–4,創(chuàng)建類,讓瀏覽器訪問

在這里插入圖片描述

–5,測試

啟動服務器

在這里插入圖片描述

打開瀏覽器訪問指定的地址::http://localhost:8080/hi

在這里插入圖片描述

二,SpringMVC

–1,概述

主要的職責:接受瀏覽器發(fā)來的請求,給瀏覽器發(fā)送響應的數(shù)據(jù)
遵循了MVC的設計模式:好處是可以把代碼松耦合
MVC的全稱:M是Model模型,用來封裝數(shù)據(jù)
  V是View視圖,用來展示數(shù)據(jù)
  C是Controller控制器,用來寫業(yè)務代碼

–2,原理

當瀏覽器發(fā)起請求,就會訪問服務器----前端控制器DispatcherServlet—處理器映射器HandlerMapping—處理器適配器

HandlerAdaptor—視圖解析器ViewResolver—視圖渲染—響應數(shù)據(jù)。

–前端控制器DispatcherServlet:: 把請求進行分發(fā),找到對應的類里的方法開始干活

–處理器映射器HandlerMapping::根據(jù)url來找到對應的類并找到對應的方法

http://localhost:8080/hello/hi 即將訪問 HelloBoot類里的 hi()

–處理器適配器HandlerAdaptor::拿到要執(zhí)行的類名和方法名,開始干活

–視圖解析器ViewResolver::解析要在瀏覽器上展示的數(shù)據(jù)

–視圖渲染:::真正的把數(shù)據(jù)在瀏覽器上展示

–3,入門案例

需求:訪問url地址,服務器返回汽車的相關數(shù)據(jù)

–1,創(chuàng)建Maven的模塊:選中工程-右鍵-New-Maven-next-輸入module的名字-finish

在這里插入圖片描述

–2,創(chuàng)建啟動類RunApp

在這里插入圖片描述

–3,創(chuàng)建汽車類

package cn.tedu;
//充當MVC模式里的M層model:封裝數(shù)據(jù)
public class Car{
    //提供屬性 + get/set/toString
    private int id;
    private String name;
    private String type;
    private String color;
    private double price;
    // get/set /toString
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    //如果沒重寫,就是用Object的toString()返回的是地址值。
    //沒重了,就是返回屬性值。
    @Override
    public String toString() {
        return "Car{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", type='" + type + '\'' +
                ", color='" + color + '\'' +
                ", price=" + price +
                '}';
    }
}

–4,創(chuàng)建類,接受瀏覽器的請求,并返回數(shù)據(jù)

在這里插入圖片描述

package cn.tedu;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//職責:接受請求+做出響應
@RestController //接受瀏覽器發(fā)來的請求
@RequestMapping("car")//規(guī)定了url的寫法
public class CarController {
    //訪問http://localhost:8080/car/find,
//在瀏覽器展示了{"id":718,"name":"保時捷","type":"Cayman T","color":"紅色","price":641000.0}
    @RequestMapping("find")
    public Car find(){
        Car c = new Car();
        c.setId(718);
        c.setName("保時捷");
        c.setType("Cayman T");
        c.setColor("紅色");
        c.setPrice(641000);
        return c;//把結(jié)果返回給了瀏覽器
    }
    //訪問http://localhost:8080/car/save ,在瀏覽器展示abc
    @RequestMapping("save")
    public String save(){
        //接受請求,并返回數(shù)據(jù)
        return "abc";
    }
    //訪問http://localhost:8080/car/get ,在控制臺打印123
    @RequestMapping("get")//規(guī)定了url的寫法
    public void get(){
        System.out.println(123);
    }
}

–5,測試

在這里插入圖片描述

總結(jié)

SpringMVC的原理?DispatcherServlet->HandlerMapping->HandlerAdaptor->ViewResolver->View

SpringMVC里用的注解?@RestController 接受請求 + 負責響應 (把數(shù)據(jù)變成JSON串)

@RequestMapping 跟url匹配規(guī)定了url的寫法

@RestController 只能出現(xiàn)在類上

@RequestMapping 可以出現(xiàn)在類上或方法上

SpringBoot的注解?@SpringBootApplication 用來作為springboot的啟動類

本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關注腳本之家更多內(nèi)容!

相關文章

最新評論