只要静态资源放在类路径下: called /static (or /public or /resources or /META-INF/resources
访问 : 当前项目根路径/ + 静态资源名
原理: 静态映射/**。
结构如下图
请求进来,先去找Controller看能不能处理。不能处理的所有请求又都交给静态资源处理器。静态资源也找不到则响应404页面
改变默认的静态资源路径(默认的直接写路径即可)
spring:mvc: #访问前缀·static-path-pattern: /res/** /resources: #静态资源static-locations: [classpath:/haha/]
默认无前缀
spring:mvc:static-path-pattern: /res/**
当前项目 + static-path-pattern + 静态资源名 = 静态资源文件夹下找
自动映射 /webjars/**
https://www.webjars.org/<dependency><groupId>org.webjarsgroupId><artifactId>jqueryartifactId><version>3.5.1version>dependency>
访问地址&#xff1a;http://localhost:8080/webjars/jquery/3.5.1/jquery.js 后面地址要按照依赖里面的包路径
• 静态资源路径下 index.html
• 可以配置静态资源路径
• 但是不可以配置静态资源的访问前缀。否则导致 index.html不能被默认访问
spring:
# mvc:
# static-path-pattern: /res/** 这个会导致welcome page功能失效resources:static-locations: [classpath:/haha/]
• controller能处理/index
即页面访问的图标
favicon.ico 放在静态资源目录下即可。
Rest风格支持&#xff08;使用HTTP请求方式动词来表示对资源的操作&#xff09;
DOCTYPE html>
<html lang&#61;"en">
<head><meta charset&#61;"UTF-8"><title>Spring-Boottitle>
head>
<body>
欢迎来到spring-Boot
<br>
<b>ResFul风格测试b>
<form action&#61;"user" method&#61;"get"><input type&#61;"submit" value&#61;"Get提交">
form>
<form action&#61;"user" method&#61;"post"><input type&#61;"submit" value&#61;"Post提交">
form>
<form action&#61;"user" method&#61;"post"><input name&#61;"_method" value&#61;"delete" type&#61;"hidden" ><input type&#61;"submit" value&#61;"delete提交">
form>
<form action&#61;"user" method&#61;"post"><input name&#61;"_method" value&#61;"put" type&#61;"hidden"><input type&#61;"submit" value&#61;"Post提交">
form>
body>
html>
后台实现
package springboot.day01.controller;import org.springframework.web.bind.annotation.*;&#64;RestController
public class HollowControler {// &#64;RequestMapping(value &#61; "/user",method &#61; RequestMethod.GET)&#64;GetMapping("/user")public String getUser(){return "Get_张三";}// &#64;RequestMapping(value &#61; "/user",method &#61; RequestMethod.POST)&#64;PostMapping("/user")public String saveUser(){return "Post_张三";}// &#64;RequestMapping(value &#61; "/user",method &#61; RequestMethod.PUT)&#64;PutMapping("/user")public String putUser(){return "Put_张三";}// &#64;RequestMapping(value &#61; "/user",method &#61; RequestMethod.DELETE)&#64;DeleteMapping("/user")public String deleteUser(){return "Delete_张三";}}
核心配置
在application.yaml中
spring:mvc:hiddenmethod:filter:enabled: true
Rest原理&#xff08;表单提交要使用REST的时候&#xff09;
• 表单提交会带上_method&#61;PUT
• 请求过来被HiddenHttpMethodFilter拦截
• 请求是否正常&#xff0c;并且是POST
• 获取到_method的值。
• 兼容以下请求&#xff1b;PUT.DELETE.PATCH
• 原生request&#xff08;post&#xff09;&#xff0c;包装模式requesWrapper重写了getMethod方法&#xff0c;返回的是传入的值。
• 过滤器链放行的时候用wrapper。以后的方法调用getMethod是调用requesWrapper的。
&#64;pathvariable(路径变量)
&#64;RequestHeader(获取请求头)
&#64;RequestParam(获取请求参数)
&#64;RequestValue(获取COOKIE值)
&#64;RequestAttriable(获取request域属性)
&#64;RequestBody(获取请求体&#xff0c;只用于post请求)
&#64;MatriVariable(矩阵变量)
代码实现
html
DOCTYPE html>
<html lang&#61;"en">
<head><meta charset&#61;"UTF-8"><title>Spring-Boottitle>
head>
<body>
欢迎来到spring-Boot
测试基本注解
<ul><a href&#61;"car/3/owner/张三?name&#61;张三&age&#61;18">&#64;pathvariable(路劲变量)a><a href&#61;"">&#64;RequestHeader(获取请求头)a><a href&#61;"">&#64;RequestParam(获取请求参数)a><a href&#61;"">&#64;RequestValue(获取COOKIE值)a><a href&#61;"">&#64;RequestAttriable(获取request域属性)a><a href&#61;"">&#64;RequestBody(获取请求体)a><a href&#61;"">&#64;MatriVariable(矩阵变量)a>
ul>
<br>
<form method&#61;"post" action&#61;"useracc">用户名:<input type&#61;"text" name&#61;"username"><br>密码:<input type&#61;"password" name&#61;"password"><br><input type&#61;"submit" value&#61;"提交">
form>
body>
html>
java
package springboot.day01.controller;import org.springframework.web.bind.annotation.*;import javax.servlet.http.COOKIE;
import java.util.HashMap;
import java.util.Map;&#64;RestController
public class ParameterTextcontroller {&#64;GetMapping("/car/{id}/owner/{username}")public Map<String,Object> getcar(&#64;PathVariable("id") Integer id,&#64;PathVariable("username") String username,&#64;PathVariable Map<String,String>pv,&#64;RequestHeader("Host") String host,&#64;RequestHeader Map<String,String> heahers,&#64;RequestParam("name") String name,&#64;RequestParam("age") int age){Map<String,Object> map&#61;new HashMap<>();map.put("id",id);map.put("username",username);map.put("pv",pv);map.put("host",host);map.put("name",name);map.put("age",age);
// map.put("headers",heahers);
// System.out.println(COOKIE);return map;}&#64;PostMapping("/useracc")public String PostMapp(&#64;RequestBody String context){return context;}
}
&#64;RequestAttribute测试
package springboot.day01.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import javax.servlet.http.HttpServletRequest;&#64;Controller
public class RequestController {&#64;RequestMapping("/attr")public String attr(HttpServletRequest request){request.setAttribute("user","张三");request.setAttribute("age",18);return "forward:success";}&#64;RequestMapping("/success")&#64;ResponseBodypublic void success(&#64;RequestAttribute("user") String user,&#64;RequestAttribute("age") int age,HttpServletRequest request){System.out.println(user);System.out.println(age);String user1 &#61; (String) request.getAttribute("user");int age1 &#61;(int) request.getAttribute("age");System.out.println(user1);System.out.println(age1);}
}
可以自动类型转换与 格式化&#xff0c;可以级联封装
样例
Pet类
package com.blb.entity;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;&#64;Data
&#64;ToString
&#64;AllArgsConstructor
&#64;NoArgsConstructor
public class Pet {private String name;private String age;}
person类
package com.blb.entity;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;&#64;Data
&#64;ToString
&#64;AllArgsConstructor
&#64;NoArgsConstructor
public class Person {private String userName;private Integer age;private Pet pet;}
前端
DOCTYPE html>
<html lang&#61;"en">
<head><meta charset&#61;"UTF-8"><title>Titletitle>
head>
<body>
测试封装POJO
<form action&#61;"/saveuser" method&#61;"post">姓名:<input type&#61;"text" name&#61;"userName"><br>年龄:<input type&#61;"text" name&#61;"age"><br>宠物姓名:<input type&#61;"text" name&#61;"pet.name"><br>宠物年龄:<input type&#61;"text" name&#61;"pet.age"><br><input type&#61;"submit" value&#61;"提交" >
form>body>
html>
controller端
package com.blb.Conreoller;import com.blb.entity.Person;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;&#64;RestController
public class PersonController {&#64;PostMapping("/saveuser")public Person saveuser(Person person){System.out.println(person);return person;}
}
3.响应处理与内容协商
数据响应的分类&#xff1a;响应页面、响应数据&#xff08;json、xml、xls、图片、音频、自定义协议数据&#xff09;
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
web场景自动引入了json场景<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-json</artifactId><version>2.3.4.RELEASE</version><scope>compile</scope></dependency>
给前端返回json数据
1.返回值解析器
1、返回值处理器判断是否支持这种类型返回值 supportsReturnType
2、返回值处理器调用 handleReturnValue 进行处理
3、RequestResponseBodyMethodProcessor 可以处理返回值标了&#64;ResponseBody 注解的。