一、@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. 【学习笔记】整体二分(BZOJ2738矩阵乘法)

    也是因为一道题才来学的... 然后就发现这道模板貌似是暑假初期在某校集训的时候的比赛题 并且好像没改= = 前置芝士 1.二分= = * CDQ分治[你要是知道CDQ分治的话这玩意就很好理解啦] *本 ...

  2. C#基础提升系列——C#集合

    C#集合 有两种主要的集合类型:泛型集合和非泛型集合. 泛型集合被添加在 .NET Framework 2.0 中,并提供编译时类型安全的集合. 因此,泛型集合通常能提供更好的性能. 构造泛型集合时, ...

  3. 英语单词Repository

    Repository 来源——查看仓库的名称内容 [root@centos7 ~]# yum repolist Loaded plugins: fastestmirror Repository bas ...

  4. MVVM MVC

    在WPF的MVVM模式中,View和ViewModel之间数据和命令的关联都是通过绑定实现的,绑定后View和ViewModel并不产生直接的依赖.具体就是View中出现数据变化时会尝试修改绑定的目标 ...

  5. c++文件拷贝

    #include<fstream>void Copyfile( char* FileSource, char* FileItem ){fstream fsCopee( FileSource ...

  6. docker 部署netcore 的关键语句

    网站容器:docker run -it --name myTestWeb -p 8080:80 -v /mnt/hgfs/my_share/core/website/:/website microso ...

  7. 测开之路二十九:Flask基础之jinja2模板

    中文文档:http://docs.jinkan.org/docs/jinja2/ 与静态资源一样,Flask默认的模板目录名为templates,如果有需要的话和static一样,要在初始化的时候声明 ...

  8. iOS OpenGL ES简单绘制三角形

    OpenGL 是用于2D/3D图形编程的一套基于C语言的统一接口. windows,Linux,Unix上均可兼容. OpenGL ES 是在OpenGL嵌入式设备上的版本, android/iOS ...

  9. 记录MNIST采用卷积方式实现与理解

    从时间上来说,这篇文章写的完了,因为这个实验早就做完了:但从能力上来说,这篇文章出现的早了,因为很多地方我都还没有理解.如果不现在写,不知道什么时候会有时间是其一,另外一个原因是怕自己过段时间忘记. ...

  10. sysenter内核入口点代码分析

    参考:http://www.mouseos.com/windows/kernel/KiFastCallEntry.html http://www.mouseos.com/windows/kernel/ ...