学习笔记——@PathVariable注解基本使用;@PathVariable注解属性;REST风格CRUD概述;实现PUT&DELETE提交方法步骤;SpringMVC处理请求数据、请求头、处理Cookie信息

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

2023-01-19

一、@PathVariable注解基本使用

1、获取URL中占位符

2、占位符语法:{}

3、实例代码:

@RequestMapping("testPathVariable/{empId}")
    public String testPathVariable(@PathVariable("empId")Integer empId){
        System.out.println(" empId = " + empId);
        return SUCCESS;
    }
<a th:href="@{/EmpController/testPathVariable/1001}">测试testPathVariable</a><br>

二、@PathVariable注解属性

1、value属性

(1)类型:String

(2)作用:设置占位符中的参数名

2、name属性

(1)类型:String

3、required属性

(1)类型:boolean

(2)作用:设置当前参数是否必须入参

①true:表示当前参数必须入参,如未入参数会报以下错误

Missing URI template variable 'empId' for method parameter of type Integer

②false:表示当前参数不必须入参,如未入参,会装配null值 

三、REST风格CRUD概述

1、REST的CRUD与传统风格CRUD对比

 

2、REST风格CRUD优势

(1)提高网站排名

排名方式:

①竞价排名

②技术排名

(2)便于第三方平台对接

四、SpringMVC环境搭建

五、REST风格CRUD练习——查询

六、实现PUT&DELETE提交方法步骤

1、注册过滤器HiddenHttpMethodFilter

2、设置表单的提交方法为POST

3、设置参数:_method=PUT或_method=DELETE

七、源码解析HiddenHttpMethodFilter


public static final String DEFAULT_METHOD_PARAM = "_method";
private String methodParam = "_method";

protected
void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { HttpServletRequest requestToUse = request; if ("POST".equals(request.getMethod()) && request.getAttribute("javax.servlet.error.exception") == null) { String paramValue = request.getParameter(this.methodParam); if (StringUtils.hasLength(paramValue)) { String method = paramValue.toUpperCase(Locale.ENGLISH); if (ALLOWED_METHODS.contains(method)) { requestToUse = new HiddenHttpMethodFilter.HttpMethodRequestWrapper(request, method); } } } filterChain.doFilter((ServletRequest)requestToUse, response); }
private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper {
        private final String method;

        public HttpMethodRequestWrapper(HttpServletRequest request, String method) {
            super(request);
            this.method = method;
        }

        public String getMethod() {
            return this.method;
        }
    }

八、SpringMVC处理请求数据

1、使用Servlet处理请求数据

(1)请求参数

Spring param = request.getParameter();

(2)请求头

request.getHeader()

(3)Cookie

request.getCookies();

2、处理请求参数

(1)默认情况:可以将请求参数名,与入参参数名一致的参数,自动入参(自动类型转换)。

(2)@RequestParam注解

①作用:如请求参数与入参参数

②属性

value:是String类型;作用:设置需要入参的参数名

name:是String类型;作用:与value属性作用一致

required:是Boilean类型;作用:设置当前参数,是否必须入参

defaultValue:是String类型;作用:当装配数值未null时,指定当前defaultValue默认值

九、处理请求头

1、语法:@RequestHeader注释

2、属性

(1)value:

①类型:String

②作用:设置需要获取请求头名称

(2)name:

①类型:String

②作用:与value属性作用一致

(3)required:

①类型:Boilean

②作用:设置当前参数,是否必须入参

(4)defaultValue:

①类型:String类型

②作用:当装配数值未null时,指定当前defaultValue默认值

十、处理Cookie信息

1、语法:@CookieValue获取Cookie数值

2、属性:同上

3、实例代码:

<a th:href="@{/setCookie}">设置Cookie</a>
<a th:href="@{/getCookie}">获取Cookie</a>
@RequestMapping("/setCookie")
    public String setCookie(HttpSession httpSession){
        System.out.println("httpSession.getId() = " + httpSession.getId());
        return SUCCESS;
    }

    @RequestMapping("/getCookie")
    public String getCookie(@CookieValue("JSESSIONID")String cookieValue){
        System.out.println("cookieValue = " + cookieValue);
        return SUCCESS;
    }

 

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Spring

“学习笔记——@PathVariable注解基本使用;@PathVariable注解属性;REST风格CRUD概述;实现PUT&DELETE提交方法步骤;SpringMVC处理请求数据、请求头、处理Cookie信息” 的相关文章