一、@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. HugeGraph图数据库--测试

    2018年百度的HugeGraph.实现了Apache TinkerPop3框架及完全兼容Gremlin查询语言.开源项目https://github.com/hugegraph HugeGraph典 ...

  2. NET Core+win10+Jenkins+Github持续集成

    本篇和上一篇NET Core+win10+Jenkins+Gogs+open ssh持续集成没什么区别,只不过源码库换成github. 这里有两点不一样的是: 获取的代码的凭证不用用户名和密码用sec ...

  3. 【leetcode】1032. Stream of Characters

    题目如下: Implement the StreamChecker class as follows: StreamChecker(words): Constructor, init the data ...

  4. django+nginx+uwsgi_cent0s7.4 部署

    django+nginx+uwsgi_cent0s7.4 部署 几条命令 # 查看是否有 uwsgi 相关的进程 ps -aux|grep "uwsgi" # 杀死有关 uwsgi ...

  5. 10.18.1 linux文本编辑器vim

    vi和vim的区别 编辑一个文本时,vi不会显示颜色,而vim会显示颜色,vi 有点类似windows记事本,简单,那么就是vim复杂编辑器,功能复杂,高亮,自动缩进(写shell/python脚本用 ...

  6. 用DELPHI中实现RAR文件解压到指定一目录

    一个RAR压缩文件,用DELPHI编的程序打开它并解压到某一目录,怎么实现的?自己搞定了例子:winrar.exe e -y C:\WINDOWS\Desktop\ghost.rar d:\ 但新的问 ...

  7. oracle11g笔记

    安装 #!/bin/bash #安装oracle110203 pageDir="/opt/tools/oracle" bdFile="/tmp/bdFile.txt&qu ...

  8. 26 October in 614

    Practice tower 有 \(N\,(2\le N\le 600000)\) 块砖,要搭一个 \(N\) 层的塔,要求:如果砖 \(A\) 在砖 \(B\) 上面,那么 \(A\) 不能比 \ ...

  9. [CSP-S模拟测试]:w(树上DP)

    题目背景 $\frac{1}{4}$遇到了一道水题,双完全不会做,于是去请教小$D$.小$D$看了${0.607}^2$眼就切掉了这题,嘲讽了$\frac{1}{4}$一番就离开了.于是,$\frac ...

  10. 【原】webpack--loaders,主要解释为什么需要loaders和注意事项

    Why需要loaders? webpack开箱即用只支持JS和JSON两种文件类型,但是比如css.less,还有目前市场上比较新的语法糖jsx,怎么处理呢? 通过Loaders去支持其他文件类型并且 ...