1、@RequestParam使用于参数上,用于将请求参数映射到指定参数变量上

例如:

 @RequestMapping(value="/hello",method=RequestMethod.GET)

 public String testPara(@RequestParam String str){

 System.out.println(str);

 return "hello";

 }

当我们访问 http://localhost:8080/springMVC/hello?str=xx 的时候,会将xx注入到testParam方法中的str中,你会看到输出xx

当然此时你在浏览器的参数名称必须跟testPara中的参数一样即必须都为str,假如你想使用不同的参数名称,就必须在@RequestParam(" anothername") str 指定

参数名,这样当你访问 http://localhost:8080/springMVC/hello?anothername=xx ,就会将xx注入到str中了。springMVC中默认就是开启 @RequestParam功能的,

你直接访问http://localhost:8080/springMVC/hello?str=xx在程序中不写@RequestParam,还是会注入到str参数中。当然我们最好还是写一下,有助于理解和利于

代码的阅读

2、@PathVariable与@RequestParam类似,但是它是用于rest风格 访问时候获取参数的方式,使用这个注解前提是在@RequestMapping中必须指定模板,不然

访问可能出现400,服务器不懂你要干嘛。例如下面的例子:

 public String testPath(@PathVariable String para)

 @RequestMapping(value="/hello/{para}",method=RequestMethod.GET) //在reqestMapping指定了模板{para}

 public String testPath(@PathVariable String para){  //参数名字一定要跟模板的名字一样,如果不想跟模板一样,可以写成这样@PathVariable("para")String other

 System.out.println(para);

 return "hello";

 }

访问:http://localhost:8080/springMv/spring/hello/anty    控制台输出参数anty

3、 @responsebody表示该方法的返回结果直接写入HTTP response body中
一般在异步获取数据时使用,在使用@RequestMapping后,返回值通常解析为跳转路径,加上@responsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中响应给客户端。比如异步获取json数据,加上@responsebody后,会直接返回json数据。使用时机:返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;

例如以下代码:

服务器端:

 @RequestMapping(value="/ajaxRequest")

 @ResponseBody

 public String ajax(String str){

 str="sb";

 return str;

 }

客户端代码:

$.ajax({

type:"POST",

async : false,

url:"spring/ajaxRequest",

success:function(data){

alert(data);

}

});

点击页面按钮访问服务器,弹出sb

4、@RequestBody

作用:

i) 该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对象上;

ii) 再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上。

使用时机:

A) GET、POST方式提时, 根据request header Content-Type的值来判断:

·     application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的数据@RequestParam, @ModelAttribute也可以处理,当然@RequestBody也能处理);

·     multipart/form-data, 不能处理(即使用@RequestBody不能处理这种格式的数据);

·     其他格式, 必须(其他格式包括application/json, application/xml等。这些格式的数据,必须使用@RequestBody来处理);

B) PUT方式提交时, 根据request header Content-Type的值来判断:

·     application/x-www-form-urlencoded, 必须;

·     multipart/form-data, 不能处理;

·     其他格式, 必须;

说明:request的body部分的数据编码格式由header部分的Content-Type指定;m

requestbody可以直接将客户端发来的ajax请求的json对象注入到服务端的对象中,但是json对象的字段必须跟后台对象的字段对应,

例如 var user{"username":"tom"} 后台的User对象必须要有username 这个字段。既然是使用异步,那么requestBody很多时候是跟responseBody搭配着一起使用!!例如下列代码:

//@RequestMapping(value="/ajaxRequest")

//@ResponseBody

//public String ajax(@RequestBody User user){

//System.out.println(user.getUsername());

//return user.getUsername();

//}

客户端代码:

var m = {

"username":'fuckyou'

};

$.ajax({

type:"POST",

async : false,

url:"spring/ajaxRequest",

dataType:"json",

contentType:"application/json; charset=utf-8",//一定要加这一句发送内容为json

data:JSON.stringify(m),//requestbody接收的是json字符串,所以得将json对象进行转换否则报415错误类型无法识别

success:function(data){

alert("return map success!");

}

});

在客户端发起请求,服务器控制台输出fuckyou,表示username字段注入到user对象

5、@ModelAttribute    一种是放在方法上面,一种是放在参数上面(比较常用的是放在参数前面)

放在方法上面:

     @ModelAttribute
public User User(User userd){
userd.setUsername("fuckg");
return userd; }
@RequestMapping(value="/hello",method=RequestMethod.GET)
public String testPara(User u){
System.out.println(u.getUsername());
return "hello";
}

在到达所有requestMapping之前 会被modelAttribute拦截,在客户端提交表单 会将对应 的属性 注入到Userd 对象中,然后springMVC默认会将usrerd对象 放入视图中,以便访问, 我们可以在hello页面 使用{user.username} 访问,

这里要注意的是 不管参数是写成userd还是写成users,都只能通过user.username访问到,spring是按照该对象类型 的小写来访问,User类对象,所以在页面就通过user访问属性。

第二种是将注解放到参数前:

 @RequestMapping("/modd") //默认使用modelAttribute,如果去掉效果还是一样
public String testModelAttr(@ModelAttribute User userqqq){
System.out.println(userqqq.getUsername());
return "md"; }
 

这里也是表单提交过来的参数 会注入到user 对象的对应属性上(这里表单提交过来的数据,不能使用@RequestBody替换ModelAttribute,requestBody常用在异步提交json数据,在这换成requestBody的话会报415错误),然后spring 也会把该对象放入视图中,在md.jsp页面,就可以通过${user.username}访问,当然这里也是一样

不管参数写成什么样,还是按照该类型的小写字母来访问,所以不是按userqqq来访问,直接按照User-->小写user访问。

尝试了一下去除ModelAttribute的效果,发现还是可用,说明,是spring 默认开启了参数前 的ModelAttribute注解,不过要用的时候还是写出来 利于代码阅读

@requestBody 和@ModelAttribute 都可以用来绑定请求数据到参数上,在我的理解范围内:requestBody主要是将异步请求ajax,json字符串绑定到对象上,requestBody

只接受json字符串,所以记得使用JSON.stringif方法将json对象转成字符串,  而ModelAttribute主要接收来自表单或者url?para参数,将参数绑定到对象上。

转自:https://blog.csdn.net/zz210891470/article/details/59719251

@RequestParam,@PathVariable,@ResponseBody,@RequestBody,@ModelAttribute学习的更多相关文章

  1. @RequestParam,@PathVariable,@RequestBody

    @RequestParam 和 @PathVariable 注解是用于从request中接收请求的,两个都可以接收参数,关键点不同的是@RequestParam 是从request里面拿取值,而 @P ...

  2. springMVC @ModelAttribute学习

    springMVC @ModelAttribute学习 博客分类: Spring   @ModelAttribute 绑定请求参数到命令对象 @ModelAttribute一个具有如下三个作用: ①绑 ...

  3. SpringMVC使用@PathVariable,@RequestBody,@ResponseBody,@RequestParam,@InitBinder

    @Pathvariable public ResponseEntity<String> ordersBack(           @PathVariable String reqKey, ...

  4. SpringMVC @RequestBody @RequestParam @PathVariable 等参数绑定注解详解

    request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: http://blog.csdn.net/walkerjong/article/details/794 ...

  5. @RequestParam、@PathVariable、 @RequestBody用法

    Get和Post请求 get方式的请求是没有请求体的.但是get有query string parameter .比如url?name=zhangsan post请求发现了两种请求体.一种是FromD ...

  6. @RequestBody和@RequestParam、@ResponseBody的使用

    一:前沿 针对spring mvc的使用,其实我不怎么熟悉的,因为我只是会用几个常用的注解,其他高深的我都不是很清楚的,而且每次用有时候还需要出查资料的,现在自己记载下吧. 二:内容 (1)这里的@R ...

  7. @RequestMapping,@ResponseBody,@RequestBody用法

    本文转载:http://blog.csdn.net/ff906317011/article/details/78552426 1.@RequestMapping 国际惯例先介绍什么是@RequestM ...

  8. @RequestParam @PathVariable

    1.Request参数 在访问各种各样网站时,经常会发现网站的URL的最后一部分形如:?xxxx=yyyy&zzzz=wwww.这就是HTTP协议中的Request参数,它有什么用呢?先来看一 ...

  9. SpringMVC常用注解實例詳解1:@Controller,@RequestMapping,@RequestParam,@PathVariable

    我的開發環境 框架:        springmvc+spring+freemarker 開發工具: springsource-tool-suite-2.9.0 JDK版本: 1.6.0_29 to ...

随机推荐

  1. zigbee 中 OSAL 事件传递机制和消息传递机制

    一.概述 OSAL (Operating System Abstraction Layer) ,翻译为"操作系统抽象层". OSAL 就是一种支持多任务运行的系统资源分配机制.OS ...

  2. 3224: Tyvj 1728 普通平衡树(新板子)

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 17048  Solved: 7429[Submit][St ...

  3. HDU 2460 Network 边双连通分量 缩点

    题意: 给出一个无向连通图,有\(m\)次操作,每次在\(u, v\)之间加一条边,并输出此时图中桥的个数. 分析: 先找出边双连通分量然后缩点得到一棵树,树上的每条边都输原图中的桥,因此此时桥的个数 ...

  4. CodeForces 567F DP Mausoleum

    本着只贴代码不写分析的题解是在耍流氓的原则,还是决定写点分析. 思路很清晰,参考的官方题解,一下文字仅对题解做一个简要翻译. 题意: 有1~n这n个数,每个数用两次.构成一个长为2n的序列,而且要求序 ...

  5. logging模块的作用以及应用场景

    一.python中的logging模块 logging模块定义的函数和类为应用程序和库的开发实现了一个灵活的事件日志系统.logging模块是Python的一个标准库模块,由标准库模块提供日志记录AP ...

  6. 深度理解STL之map、set

    课程设计做了这个一直没有整理(搬运 set算是关键字和相同的特殊map set应该更加被强调理解为“集合”,而集合所涉及的操作并.交.差等,即STL提供的如交集set_intersection().并 ...

  7. acmer之ubuntu下安装Eclipse

    ubuntu是acmer常用的系统,配置起CB还是比较简单的三行命令就OK了 //Current stable version of Code::Blocks IDE (16.01) //To ins ...

  8. POJ 2051 Argus

    Argus Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8782   Accepted: 3985 Description ...

  9. cronolog切割apache和tomcat日志

    cronolog切割apache和tomcat日志 http://cronolog.org tar zxvf cronolog-1.6.2.tar.gzcd cronolog-1.6.2./confi ...

  10. JDBC 学习笔记(八)—— ResultSet

    JDBC 使用 ResultSet 来封装 SQL 的查询结果,可以将 ResultSet 类比为数据库表的查询结果. 它拥有如下两个性质: 可滚动. 可更新. 这两个性质,是在创建 Statemen ...