一、@RequestBody请求体

注意请求体只有form表单才有,而对于链接来说不使用

1)、在Controller中写

@RequestBody String body是基本用法

另外可以封装对象@RequestBody Employee employee是高级用法

@requestBody接收的是前端传过来的json字符串

写在参数位置

@RequestMapping("/testRequestBody")
public String testRequestBody(@RequestBody Employee employee){
System.out.println("请求体:"+employee);
return "success";
}

2)、在页面中写,$.ajax();//js对象(object)转json(string)

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<%
pageContext.setAttribute("ctp", request.getContextPath());
%>
</head>
<script type="text/javascript" src="scripts/jquery-1.9.1.min.js"></script>
<body>
<form action="${ctp }/test02" method="post"
enctype="multipart/form-data">
<input name="username" value="tomcat" /> <input name="password"
value="123456"> <input type="file" name="file" /> <input
type="submit" />
</form>
<a href="${ctp }/testRequestBody">ajax发送json数据</a>
</body>
<script type="text/javascript">
$("a:first").click(function() {
//点击发送ajax请求,请求带的数据是json
var emp = {
lastName : "张三",
email : "aaa@aa.com",
gender : 0
};
//alert(typeof emp);
//js对象(object)转json(string)
var empStr = JSON.stringify(emp);
//alert(typeof empStr);
$.ajax({
url : '${ctp}/testRequestBody',
type : "POST",
data : empStr,
contentType : "application/json",
success : function(data) {
alert(data);
}
});
return false;
});
</script>
</html>

二、@PathVariable获取路径参数(这个要求映射地址是restful风格)

看一个例子,如果我们需要获取Url=localhost:8080/hello/id/name中的id值和name值,实现代码如下:

@RestController
public class HelloController { @RequestMapping(value="/hello/{id}/{name}",method= RequestMethod.GET)
public String sayHello(@PathVariable("id") Integer id,@PathVariable("name") String name){
return "id:"+id+" name:"+name;
}
}

以上,通过@PathVariable注解来获取URL中的参数时的前提条件是我们知道url的格式时怎么样的。只有知道url的格式【/传参(value后台指定了key)】【/并列传参】,我们才能在指定的方法上通过相同的格式获取相应位置的参数值。

一般情况下,url的格式为:localhost:8080/hello?id=98,【?用来传第一个参数(key=value)】【&并列传参】这种情况下该如何来获取其id值呢,这就需要借助于@RequestParam来完成了

三、@RequestParam

后端不以表单形式从前端获取值

1.在浏览器中输入地址:localhost:8080/hello?id=1000,可以看到如下的结果:

2.当我们在浏览器中输入地址:localhost:8080/hello?id ,即不输入id的具体值,此时返回的结果为null。具体测试结果如下:

3.但是,当我们在浏览器中输入地址:localhost:8080/hello ,即不输入id参数,则会报如下错误:

@RequestParam注解给我们提供了这种解决方案,即允许用户不输入id时,使用默认值,具体代码如下:

@RestController
public class HelloController {
@RequestMapping(value="/hello",method= RequestMethod.GET)
//required=false 表示url中可以不穿入id参数,此时就使用默认参数
public String sayHello(@RequestParam(value="id",required = false,defaultValue = "1") Integer id){
return "id:"+id;
}
}

四、@PathVariable和@RequestParam一起用

   @Autowired
DomOwnerResService domOwnerResService2;
@ApiOperation(value = "查询资源种类树max", notes = "针对属主拥有的资源的查询操作")
@RequestMapping(value="/queryallrestype/{domId}",method = RequestMethod.GET)
public ResultUtils queryAllRes(@PathVariable(value="domId",required = true) Integer domId,
@RequestParam(value="ownerId",required = true) Integer ownerId,
@RequestHeader(value = "Validate", required = true) String validate){
}

对于请求地址Request URL

http://localhost:8090/queryallrestype/1?ownerId=1
可以看到用了@PathVaribale的参数都用[/]并列传参且不用写key,
后面用了@ResquestParam的参数第一个用[?]传参
补充一点:
如果有多个@ResquestParam
后面并列用&传参:如http://localhost:8090/queryallrestype/1?ownerId=1&xxx=2&yyy=3

SpringMvc获取前端的数据@RequestBody请求体/@PathVaribale/@RequestParam【支持Ajax】的更多相关文章

  1. springMVC获取用户的数据

    打算记录网站的访问信息,没有眉目,下记下参考. SpringMVC-获得用户请求数据

  2. 使用restTemplate发送post请求,传入参数是在requestBody请求体中,以json形式传输

    @PostMapping public ResponseResult add(User user){ HttpHeaders httpHeaders = new HttpHeaders(); Medi ...

  3. SpringMVC 02: SpringMVC响应get和post请求 + 5种获取前端数据的方式

    响应get和post请求 SpringMVC中使用@RequestMapping注解完成对get请求和post请求的响应 项目结构和配置文件与SpringMVC博客集中的"SpringMVC ...

  4. springboot 服务端获取前端传过来的参数7种方式

    下面为7种服务端获取前端传过来的参数的方法  1.直接把表单的参数写在Controller相应的方法的形参中,适用于GET 和 POST请求方式 这种方式不会校验请求里是否带参数,即下面的userna ...

  5. body-parser Node.js(Express) HTTP请求体解析中间件

    body-parser Node.js(Express) HTTP请求体解析中间件 2016年06月08日     781     声明 在HTTP请求中,POST.PUT和PATCH三种请求方法中包 ...

  6. javaWeb - 2 — ajax、json — 最后附:后台获取前端中的input type = "file"中的信息 — 更新完毕

    1.ajax是什么? 面向百度百科一下就知道了,这里就简单提炼一下 Ajax即Asynchronous Javascript And XML(异步JavaScript和XML).当然其实我们学的应该叫 ...

  7. ajax post data 获取不到数据,注意 content-type的设置

    ajax post  data  获取不到数据,注意 content-type的设置 .post/get关于 jQuery data 传递数据.网上各种获取不到数据,乱码之类的.好吧今天我也遇到了,网 ...

  8. ajax post data 获取不到数据,注意 content-type的设置 、post/get

    ajax post  data  获取不到数据,注意 content-type的设置 .post/get 关于 jQuery data 传递数据.网上各种获取不到数据,乱码之类的. 好吧今天我也遇到了 ...

  9. ajax post data 获取不到数据

    ajax post  data  获取不到数据,注意 content-type的设置 .post/get关于 jQuery data 传递数据.网上各种获取不到数据,乱码之类的.好吧今天我也遇到了,网 ...

随机推荐

  1. 《嵌入式软件设计基础——基于ARM Cortex—M3》读书笔记

    此书有点深,记录点自己能够看懂的. 1.内存管理一章:讲到变量的类型.生存周期.内存分配. auto static register 局部变量,全局变量 malloc free 内存碎片,消除内存池的 ...

  2. jquery自带的排序方法(js也是)

    jquery.sort()   js.sort() <!DOCTYPE html> <html>   <head>     <meta charset=&qu ...

  3. Web核心之最简单最简单最简单的登录页面

    需求分析: 在登录页面提交用户名和密码 在Servlet中接收提交的参数,封装为User对象,然后调用DAO中的方法进行登录验证 在DAO中进行数据库查询操作,根据参数判断是否有对象的用户存在 在Se ...

  4. 【leetcode】564. Find the Closest Palindrome

    题目如下: 解题思路:既然是要求回文字符串,那么最终的输出结果就是对称的.要变成对称字符串,只要把处于对称位置上对应的两个字符中较大的那个变成较小的那个即可,假设n=1234,1和4对称所以把4变成1 ...

  5. vim编辑器的使用技巧——忽略字母大小写

    一忽略字母大小写临时生效 底行模式 底行模式下输入set  ic 注意ic是ignorecase的缩写 命令模式 命令模式进行关键字搜索 二忽略字母大小写永久生效 保存到配置文件里面,默认是没有此配置 ...

  6. [杂题]:B/b(二分答案)

    题目传送门(内部题53) 输入格式 第二行$2$个整数表示$n,m$.接下来$m$行每行两个整数,描述一个点对$(x_i,y_i)$. 输出格式 一个整数,表示最短距离. 样例 样例输入: 6 21 ...

  7. NGINX配置之二: nginx location proxy_pass 后面的url 加与不加/的区别.

    这里我们分4种情况讨论 这里我们请求的网站为:192.168.1.123:80/static/a.html 整个配置文件是 server{ port 80, server name 192.168.1 ...

  8. vue 页面添加水印 ts

    "use strict"; // tslint:disable-next-line: only-arrow-functions const setWatermark: (str: ...

  9. 仿移动端触摸滑动插件swiper,的简单实现

    ​ /** * @author lyj * @Date 2016-02-04 * @Method 滑动方法 针对一个大容器内部的容器做滑动封装 * @param * args args.swipeDo ...

  10. python赞乎--学习开发