Day5-SpringBoot-Controller层注解
@PathVariable、@RequestHeader、@ModelAttribute、@RequestParam、@MatrixVariable、@CookieValue、@RequestBody
后
package com.flya.demo.controller;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
/**
* Created with IntelliJ IDEA.
*
* @author: 风离
* @Date: 2021/07/13/22:17
* @Description:
*/
@RestController
public class 注解Controller {
@GetMapping("/test/{uid}/{username}/{password}")
public Map<String,Object> hashMap(@PathVariable Map<String,String> test
,@PathVariable("uid") String uid,@RequestParam("flLink")String blogLink
,@RequestHeader Map<String,String> allHeader,@RequestHeader("User-Agent") String agent
,@CookieValue("Idea-8296e76f") String cookie
){
Map<String,Object> test01 =new HashMap<>();
test01.put("allPathVariable",test);
test01.put("PathVariable-uid",uid);
test01.put("flLink",blogLink);
test01.put("allHeader",allHeader);
test01.put("Header-User-Agent",agent);
test01.put("allCookie",cookie);
return test01;
}
}
前
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>你好,风离</h2>
<h2>GET POST PUT DELETE</h2>
<a href="/test/1/风离/rewq4321?flLink='https://flya.top/'">点我~</a>
<a href="/person/3/张三">点我跳转到动态路径获取</a>
<form method="get" action="/person">
<h3>请输入用户pid: <input type="text" name="pid"></h3>
<h3>请输入用户pname: <input type="text" name="pname"></h3>
<h3><input type="submit" value="提交"></h3>
</form>
<form method="get" action="/user">
<input type="submit" value="提交GET请求">
</form>
<form method="post" action="/user">
<input type="submit" value="提交POST请求">
</form>
<form method="post" action="/user">
<input name="m" hidden value="PUT"> <!-- 这里可以自定义 -->
<input type="submit" value="提交PUT请求">
</form>
<form method="post" action="/user">
<input name="_method" hidden value="DELETE">
<input type="submit" value="提交DELETE请求">
</form>
</body>
</html>
测试截图
// 20210713223935
// http://localhost:9999/test/1/%E9%A3%8E%E7%A6%BB/rewq4321?flLink=%27https://flya.top/%27
{
"flLink": "'https://flya.top/'",
"Header-User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
"PathVariable-uid": "1",
"allCookie": "74dc900c-2142-4123-a0e4-5327fb4f77ac",
"allPathVariable": {
"uid": "1",
"username": "风离",
"password": "rewq4321"
},
"allHeader": {
"host": "localhost:9999",
"connection": "keep-alive",
"sec-ch-ua": "\" Not;A Brand\";v=\"99\", \"Google Chrome\";v=\"91\", \"Chromium\";v=\"91\"",
"sec-ch-ua-mobile": "?0",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"sec-fetch-site": "same-origin",
"sec-fetch-mode": "navigate",
"sec-fetch-user": "?1",
"sec-fetch-dest": "document",
"referer": "http://localhost:9999/index.html",
"accept-encoding": "gzip, deflate, br",
"accept-language": "zh-CN,zh;q=0.9",
"cookie": "Idea-8296e76f=74dc900c-2142-4123-a0e4-5327fb4f77ac"
}
}
thymeleaf
视图解析:SpringBoot默认不支持 JSP,需要引入第三方模板引擎技术实现页面渲染
基本语法
1、表达式
表达式名字 | 语法 | 用途 |
---|---|---|
变量取值 | ${…} | 获取请求域、session域、对象等值 |
选择变量 | *{…} | 获取上下文对象值 |
消息 | #{…} | 获取国际化等值 |
链接 | @{…} | 生成链接 |
片段表达式 | ~{…} | jsp:include 作用,引入公共页面片段 |
2、字面量
文本值: ‘one text’ , ‘Another one!’ **,…**数字: 0 , 34 , 3.0 , 12.3 **,…**布尔值: true , false
空值: null
变量: one,two,… 变量不能有空格
3、文本操作
字符串拼接: +
变量替换: |The name is ${name}|
4、数学运算
运算符: + , - , * , / , %
5、布尔运算
运算符: and , or
一元运算: ! , not
6、比较运算
比较: > , < , >= , <= ( gt , lt , ge , le **)**等式: == , != ( eq , ne )
7、条件运算
If-then: (if) ? (then)
If-then-else: (if) ? (then) : (else)
Default: (value) ?: (defaultvalue)
属性优先级
thymeleaf使用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(ThymeleafProperties.class)
@ConditionalOnClass({ TemplateMode.class, SpringTemplateEngine.class })
@AutoConfigureAfter({ WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class })
public class ThymeleafAutoConfiguration { }
自动配好的策略
-
1、所有thymeleaf的配置值都在 ThymeleafProperties
-
2、配置好了 SpringTemplateEngine
-
3、配好了 ThymeleafViewResolver
-
4、我们只需要直接开发页面
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html"; //xxx.html
页面开发
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1 th:text="${msg}">Hello!Thymeleaf!</h1>
<a href="" th:href="${myWeb}">点我去我的博客</a>
</body>
</html>
package com.flya.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
/**
* Created with IntelliJ IDEA.
*
* @author: 风离
* @Date: 2021/07/13/19:49
* @Description:
*/
@Controller
public class ThymeleafController {
@GetMapping("/thymeleaf")
public String thymeleaf(Model model){
// Model 中的数据会被放在请求域中
model.addAttribute("msg","你好呀");
model.addAttribute("myWeb","https://flya.top");
return "HelloThymeleaf";
}
}