南平建设网站,怎样建立和设计公司网站,怎么上百度推广产品,版式网站有哪些一、RequestParam注解
作用#xff1a;用于将指定的请求参数赋值给方法中的形参。 属性#xff1a; 1#xff09;value#xff1a;请求参数名#xff08;必须配置#xff09; 2#xff09;required#xff1a;是否必需#xff0c;默认为 true#xff0c;即请求中必须…一、RequestParam注解
作用用于将指定的请求参数赋值给方法中的形参。 属性 1value请求参数名必须配置 2required是否必需默认为 true即请求中必须包含该参数如果没有包含将会抛出异常可选配置 3defaultValue设置默认值如果设置了该值required 将自动设为 false无论你是否配置了required配置了什么值都是 false可选配置
用法 ResponseBodyGetMapping(/RequestParam)public Map test(RequestParam(username) String username,RequestParam(password) ListString password){Map mapnew HashMap();map.put(username,username);map.put(password,password);return map;}注意GET请求时请求的参数是拼接在请求地址URL后面的 也可以通过一个RequestParam注解与Map集合类型参数同时获取多个参数 ResponseBodyGetMapping(/RequestParam)public Map test(RequestParam MapString,String map1){Map mapnew HashMap();map.put(test,map1);return map;}二、PathVariable注解
1、get方式的安全性较Post方式要差些包含机密信息的话建议用Post数据提交方式 2、在做数据查询时建议用Get方式而在做数据添加、修改或删除时建议用Post方式post需要token来防止跨站请求漏洞。
PathVariable注解在应用时在RequestMapping请求路径中将需要传递的参数用花括号{}括起来然后通过PathVariable(“参数名称”)获取URL中对应的参数值。如果PathVariable标明参数名称则参数名称必须和URL中的参数名称保持一致。 用法 ResponseBodyGetMapping(/PathVariable/{name}/{age})public Map test(PathVariable(name) String name,PathVariable(age) Integer age){Map mapnew HashMap();map.put(name,name);map.put(age,age);return map;}也可以使用一次注解获取多个参数 ResponseBodyGetMapping(/PathVariable/{name}/{age})public Map test(PathVariable MapString,String map3){Map mapnew HashMap();map.put(params,map3);return map;}三、RequestBody注解
主要用来接收前端传递给后端的json字符串中的数据也就是请求体中的数据GET方式无请求体所以使用**RequestBody接收数据时前端不能使用GET方式提交数据而是用POST方式进行提交。**
在后端的同一个接收方法里RequestBody与RequestParam()可以同时使用RequestBody最多只能有一个而RequestParam()可以有多个。 ResponseBodyPostMapping(/save)//获取请求体参数public String getRequestBody(RequestBody String body){return body;}四、RequestAttribute注解
获取请求域中所保存的属性的值。 用法首先向浏览器发起goto请求并添加自定义值到请求域中再转发到success请求去获取这些请求域中的值。 GetMapping(/goto)public String toSuccess(HttpServletRequest request){request.setAttribute(msg,成功了!);request.setAttribute(code,200);return forward:/success;}//使用RequestAttribute注解获取请求域中的值与//不使用注解获取请求域中的值GetMapping(/success)public String success(RequestAttribute(msg)String msg,RequestAttribute(code) Integer code,HttpServletRequest request){Object msg1 request.getAttribute(msg);Map mapnew HashMap();map.put(msg,msg);map.put(code,code);System.out.println(map);System.out.println(msg1);return success;}