文章目錄
- 一,SpringMVC框架
- --1,概述
- --2,入門案例
- 專案結構
- 創建啟動類
- 創建CarController類
- 測驗
- 二,springmvc框架決議請求引數
- --1,get方式
- 決議復雜的引數
- 創建Car類
- --2,restful方式
- 在IDEA里創建后端代碼UserController 類
- 在HB里創建前端html代碼
- 測驗
- 練習:
- 需求
- 修改前端頁面
- 修改后端代碼,創建類
- --3,post方式
- 準備表單
- 準備類,決議請求資料
- 擴展:入庫
一,SpringMVC框架
–1,概述
主要作用: 接受請求 + 做出回應
使用步驟:
1, 匯入jar包 (核心: spring-webmvc.jar),被springboot簡化了
2, 創建啟動類,啟動服務器
3, 準備服務器里的資源,可以被瀏覽器訪問
–2,入門案例
專案結構

創建啟動類
package cn.tedu.hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication//springboot提供的,表示自己是一個啟動類,用來啟動服務器
public class RunApp {
public static void main(String[] args) {
//SpringApplication是springboot提供的,啟動指定類
SpringApplication.run(RunApp.class);
}
}
創建CarController類
package cn.tedu.hello;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//需求: http://localhost:8080/car/get
@RestController//是springmvc提供的,可以被瀏覽器訪問
@RequestMapping("car")//是springmvc提供的,規定了瀏覽器訪問這個類的方式
public class CarController {
//訪問方式:http://localhost:8080/car/get
@RequestMapping("get")
public void get(){
System.out.println("瀏覽器訪問成功!");
}
}
測驗
打開瀏覽器訪問: http://localhost:8080/car/get
二,springmvc框架決議請求引數
–1,get方式
把資料用?拼接在地址欄中,多資料之間用& 連接 :
http://localhost:8080/car/add?id=10
決議復雜的引數
package cn.tedu.hello;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//需求: http://localhost:8080/car/get
/*
總結:
1, SpringMVC可以接受請求,并決議請求引數
只要在方法的引數串列中宣告就可以:public void add(int id,String name)
2, 為了避免400或者500的例外,最好把引數串列的引數型別改成參考型別
改后:public void add(Integer id,String name)
3, 引數串列可以提供一個物件,框架可以決議引數,并且可以把引數直接給物件的屬性賦值
public void save2(Car c)
*/
@RestController//是springmvc提供的,可以被瀏覽器訪問
@RequestMapping("car")//是springmvc提供的,規定了瀏覽器訪問這個類的方式
public class CarController {
@RequestMapping("get")
public void get(){
System.out.println("瀏覽器訪問成功!");
}
//springmvc決議get方式的請求引數
//練習1: http://localhost:8080/car/add?id=10
@RequestMapping("add")
public void add(Integer id){
//引數串列的寫法:1,引數型別必須和請求的引數型別一致 2,引數名稱必須和請求的引數名稱一致
System.out.println("瀏覽器訪問成功add!"+id);
}
//練習2: http://localhost:8080/car/add2?id=10&name=jack
@RequestMapping("add2")
public void add(Integer id,String name){
System.out.println("瀏覽器訪問成功add!"+id+name);
}
//練習3: http://localhost:8080/car/save?id=1&color=red&price=100
//HandlerMapping:處理器映射器,拿著地址欄的訪問方式找到哪個類哪個方法
//Map<String,Object> {"/car/save", new CarController().save()}
@RequestMapping("save")
public void save(Integer id,String color,Double price){
//基本型別的引數,訪問時必須傳參否則400 500例外,
//參考型別不傳入引數也行,就會用默認值null
//優化1:推薦使用參考型別,因為規避了例外的發生!!!
System.out.println(id+color+price);
}
//練習4:http://localhost:8080/car/save2?id=1&color=red&price=100&pinpai=BMW&type=X7
//框架的作用:1,決議了請求引數 2,把決議到的引數呼叫c.setXxx()設定值
@RequestMapping("save2")
public void save2(Car c){
System.out.println(c);
//Car{id=1, color='red', price=100.0, pinpai='BMW', type='X7'}
}
}
創建Car類
package cn.tedu.hello;
//http://localhost:8080/car/save2?
// id=1&color=red&price=100&pinpai=BMW&type=X7
//這個類用來存,springmvc框架決議到的請求引數
public class Car {
private Integer id;
private String color;
private Double price;
private String pinpai;
private String type ;
//set get tostring --右鍵-generate
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}
public String getPinpai() {
return pinpai;
}
public void setPinpai(String pinpai) {
this.pinpai = pinpai;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String toString() {
return "Car{" +
"id=" + id +
", color='" + color + '\'' +
", price=" + price +
", pinpai='" + pinpai + '\'' +
", type='" + type + '\'' +
'}';
}
}
–2,restful方式
在IDEA里創建后端代碼UserController 類
package cn.tedu.hello;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//需求:決議restful風格提交的資料,簡化了get提交資料的寫法
//普通get方式:http://localhost:8080/user/insert?id=1&name=張三&age=18
//restful方式:http://localhost:8080/user/insert/1/張三/18
@RestController
@RequestMapping("user")
public class UserController {
//決議get資料:http://localhost:8080/user/insert?id=1&name=張三&age=18
@RequestMapping("insert")
public Object insert(Integer id,String name,Integer age){
return id+name+age;
}
//決議restful資料:http://localhost:8080/user/insert2/1/張三/18
//步驟:1,利用{}獲取地址欄中的引數,個數和順序要和地址欄里的引數匹配
//2,利用@PathVariable注解,獲取花括號中間的變數的值
@RequestMapping("insert2/{id}/{name}/{age}")
public void insert2(@PathVariable Integer id,
@PathVariable String name,
@PathVariable Integer age){
System.out.println(id+name+age);
}
}
在HB里創建前端html代碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>前后端整合</title>
</head>
<body>
<a href="http://localhost:8080/user/insert">普通訪問</a>
<a href="http://localhost:8080/user/insert?id=10&name=rose&age=20">普通的get提交方式</a>
<a href="http://localhost:8080/user/insert2/10/rose/20">restful方式</a>
</body>
</html>
測驗

練習:
需求
練習:
http://localhost:8080/car/save2?id=1&color=red&price=100&pinpai=BMW&type=X7
1,決議瀏覽器發來的請求引數
2,給瀏覽器回傳資料
修改前端頁面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>前后端整合</title>
</head>
<body>
<a href="http://localhost:8080/user/insert">普通訪問</a>
<a href="http://localhost:8080/user/insert?id=10&name=rose&age=20">普通的get提交方式</a>
<a href="http://localhost:8080/user/insert2/10/rose/20">restful方式</a>
<a href="http://localhost:8080/car/save3?id=1&color=red&price=100&pinpai=BMW&type=X7">點我獲取汽車資料</a>
</body>
</html>
修改后端代碼,創建類
package cn.tedu.hello;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("car")
public class CarController2 {
//1,決議瀏覽器發來的請求引數
//http://localhost:8080/car/save3?id=1&color=red&price=100&pinpai=BMW&type=X7
@RequestMapping("save3")
public Object save3(Car c){
//TODO 把決議到的請求引數 getXxx()入庫--jdbc
//pom里加jdbc的坐標
//在資料庫里創建car表(提供id,color,price,pinpai,type欄位)
//注冊驅動 獲取資料庫連接 獲取傳輸器 執行SQL 決議結果集 釋放資源
//{"id":1,"color":"red","price":100.0,"pinpai":"BMW","type":"X7"}
return c;
}
}
–3,post方式
準備表單

準備類,決議請求資料
擴展:入庫
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/300232.html
標籤:java
上一篇:java 自動拆裝箱特性
