記事ディレクトリ
- リクエスト処理-一般的なパラメータアノテーションの使用
- `PathVariable`はパス変数アノテーションを取得します
- リクエストヘッダー`@RequestHeader`を取得します
- `@RequestParam`リクエストパラメータを取得します(疑問符の後のパラメータを参照、url?a = 1&b = 2)
- cookie値を取得するための`@cookieValue`
- @RequestBodyリクエスト本文のデータを取得します(POSTリクエスト)
- リクエスト処理-@RequestAttributeはリクエストフィールドの属性を取得します
- MatrixVariable与UrlPathHelper
- オフトピック
リクエスト処理-一般的なパラメータアノテーションの使用
注釈:
@PathVariable
パス変数@RequestHeader
リクエストヘッダーを取得する@RequestParam
リクエストパラメータを取得します(疑問符の後のパラメータを参照、url?a = 1&b = 2)@CookieValue
Cookieの値を取得する@RequestAttribute
リクエストドメイン属性を取得する@RequestBody
リクエスト本文を取得[POST]@MatrixVariable
行列変数@ModelAttribute
これが私たちに起こったことです
PathVariable
パス変数アノテーションを取得する
PathVariableアノテーションを見てください
パラメータの値がmapの場合、彼はkv値をマップへのパスに配置します
@RestController
public class ParameterTestController {
@GetMapping("/car/{id}/owner/{name}")
public Map<String,Object> getCar(@PathVariable("id")Integer id ,
@PathVariable("name") String name,
@PathVariable Map<String,String> pv) {
Map<String,Object> map = new HashMap<>();
map.put("id",id);
map.put("name",name);
map.put("pv",pv);
return map;
}
}
このマップは、すべての値
を取得して取得しようとします
リクエストヘッダーを取得する@RequestHeader
とった
@RequestParam
リクエストパラメータを取得します(疑問符の後のパラメータを参照、url?a = 1&b = 2)
マップは上記と同じで、すべてがカプセル化されています
@CookieValue
Cookieの値を取得する
@CookieValueのコメントは
、Cookieの種類を宣言して、すべてのCookieを取得できることを意味している可能性があります。
一部のブラウザにはCookieがありません。ここでは、心配しないでください。
@RequestBodyリクエスト本文のデータを取得します(POSTリクエスト)
とった
リクエスト処理-@RequestAttributeはリクエストフィールドの属性を取得します
@GetMapping("/goto")
public String goToPage(HttpServletRequest request){
request.setAttribute("msg","成功了...");
request.setAttribute("code",200);
return "forward:/success"; //转发到 /success请求
}
@ResponseBody
@GetMapping("/success")
public Map success(@RequestAttribute(value = "msg",required = false) String msg,
@RequestAttribute(value = "code",required = false)Integer code,
HttpServletRequest request){
Object msg1 = request.getAttribute("msg");
Map<String,Object> map = new HashMap<>();
Object hello = request.getAttribute("hello");
Object world = request.getAttribute("world");
Object message = request.getAttribute("message");
map.put("reqMethod_msg",msg1);
map.put("annotation_msg",msg);
map.put("hello",hello);
map.put("world",world);
map.put("message",message);
return map;
}
このfalseの値は、msgアイテムがないことです。
MatrixVariable与UrlPathHelper
-
構文:リクエストパス:
/cars/sell;low=34;brand=byd,audi,yd
この種類のバンド;はマトリックス変数です -
SpringBootは、デフォルトで行列変数の機能を無効にします
- 手動開封:原則。パスの処理。UrlPathHelperのremoveSemicolonContentはfalseに設定されており、マトリックス変数をサポートできるようになっています。
-
マトリックス変数には、解析するURLパス変数が必要です
なぜ行列変数のようなものがあるのですか?
ページ開発、Cookieが無効になっていますが、セッションでコンテンツを使用するにはどうすればよいですか?
セッション
session.set(a、b)—>
jsessionid—>
保存するものはcookiecookieに保存します
---->リクエストを送信するたびに実行します。
サーバーはCookieを受信すると、jsessionidに従ってセッションオブジェクトを検索し、getメソッドを使用して保存したものを検索できます。
ただし、Cookieがセッションに保存されているものの使用を許可されていない場合は、
この問題を解決するために、URLの書き換えが発生します。Cookieの値をURLに書き込みます。
通常の変数と区別するために、;を使用し
て間隔urlを書き換えます。/abc;jsesssionid= xxxxは、マトリックス変数を使用してCookieの値を渡します。
たとえば、/ boss / 1; age = 20/2; age = 20
1; age = 20
セミコロンの前がアクセスパスで、セミコロンの後がマトリックス変数です。
テスト
@GetMapping("/cars2/{path}")
public Map carsSell(@MatrixVariable("low") Integer low,
@MatrixVariable("brand") List<String> brand,
@PathVariable("path") String path){
Map<String,Object> map = new HashMap<>();
map.put("low",low);
map.put("brand",brand);
map.put("path",path);
return map;
}
オープンマトリックス変数
package com.example.springboot_study_02.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;
import java.util.HashMap;
import java.util.Map;
@Configuration(proxyBeanMethods = false)
public class WebConfig {
//implements WebMvcConfigurer
@Bean
public WebMvcConfigurer webMvcConfigurer() {
return new WebMvcConfigurer() {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
// 不移除;后面的内容。矩阵变量功能就可以生效
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
};
}
// @Override
// public void configurePathMatch(PathMatchConfigurer configurer) {
//
// UrlPathHelper urlPathHelper = new UrlPathHelper();
// // 不移除;后面的内容。矩阵变量功能就可以生效
// urlPathHelper.setRemoveSemicolonContent(false);
// configurer.setUrlPathHelper(urlPathHelper);
// }
}
ブロッカーをオンにすることを忘れないでください
spring:
resources:
static-locations: [classpath:/gb/,classpath:/gb1/]
mvc:
hiddenmethod:
filter:
enabled: true
オフトピック
今日は2022年3月25日です。
初日のメモは3月20日に書かれたので、5日間で30話のオンラインコースしか見ていませんでした。学習効率は比較的悪いと言えます
。学校の授業です。本当に3年生には行きたくないです。今でも毎日たくさんの学校のものに頼っています。卒業しないと飢えて死んでしまうのではないかと思います
。デザインモードから多く
のことを学びました。Xiaoyingの先輩が書いたものは私のものよりはるかに優れていますが、私の最初の記事は2日で始まったばかりで、Xiaoyingの先輩よりもランクが高く、受け入れられません。