[@Controller]3 详解@CookieValue,@PathVariable,@RequestBody,@RequestHeader,@RequestParam

 
下列参数一般都和@RequestMapping配合使用。

 

A、@CookieValue

org.springframework.web.bind.annotation.CookieValue

public @interface CookieValue

Annotation which indicates that a method parameter should be bound to an HTTP cookie. Supported for annotated handler methods in Servlet and Portlet environments.

这个注释表示一个方法参数绑定到一个HTTP cookie。支持Servlet和Portlet环境。

The method parameter may be declared as type Cookie or as cookie value type (String, int, etc).

这个方法的参数可声明为Cookie类型或String, int等。

A.1、@CookieValue的属性

String value

The name of the cookie to bind to.

绑定的cookie名称。

boolean required

Whether the header is required.

Default is true, leading to an exception being thrown in case the header is missing in the request. Switch this to false if you prefer a null in case of the missing header.

Head是否需要。默认是true,请求中头丢失将抛出一个异常。False,请求中头丢失将返回null。

Alternatively, provide a defaultValue, which implicitly sets this flag to false.

因此,提供一个defaultValue。

String defaultValue

The default value to use as a fallback. Supplying a default value implicitly sets required() to false.

当required为false,请求中头丢失将返回这个值。

 

B、@PathVariable

Annotation which indicates that a method parameter should be bound to a URI template variable. Supported for RequestMapping annotated handler methods in Servlet environments.

    这个参数指出方法的一个参数绑定到一个URI template变量。在Servlet环境中的被@RequestMapping注释的处理器方法。

B.1@PathVariable的属性

value

The URI template variable to bind to.

绑定URI template变量。

举例说明

@Controller

public class HelloWorldController {    @RequestMapping("/helloWorld/{userId}")

public String helloWorld(ModelMap model,@PathVariable("userId") String userId) {

model.addAttribute("attributeName", userId);

return "helloWorld";

}

}

当URI template变量和方法的参数名称一样时,可以省略value的定义,@PathVariable达到同样的效果。

 

C、@RequestBody

Annotation which indicates that a method parameter should be bound to the web request body. Supported for annotated handler methods in Servlet environments.

这个注释它指示一个方法的参数绑定到一个web请求的body。它支持Servlet环境中的注释处理器方法。

举例说明

@Controller

public class HelloWorldController {

@RequestMapping("/hello.do")

public String helloWorld(Model model,@RequestBody String reqBody) {

model.addAttribute("message", reqBody);

return "helloWorld";

}

}

这时这个参数reqBody的值是请求页面的form表单的所有值。

D、@ RequestHeader

Annotation which indicates that a method parameter should be bound to a web request header. Supported for annotated handler methods in Servlet and Portlet environments.

这个注释它指示一个方法的参数绑定到一个web请求的头信息。它支持Servlet和Portlet环境中的注释处理器方法。

D.1、@ RequestHeader的属性

String defaultValue

The default value to use as a fallback.

默认返回值。

Boolean required

Whether the header is required.

是否需要header。

String value

The name of the request header to bind to.

绑定的请求头名称。

举例说明

@Controller

public class HelloWorldController {

@RequestMapping("/hello.do")

public String helloWorld(Model model,@RequestHeader("Accept") String info) {

model.addAttribute("message", info);

return "helloWorld";

}

}

这时这个参数info将获得请求的Accept头信息。

 

E@RequestParam

org.springframework.web.bind.annotation.RequestParam

Annotation which indicates that a method parameter should be bound to a web request parameter. Supported for annotated handler methods in Servlet and Portlet environments.

这个参数指出一个方法的参数应绑定到一个web请求的参数。支持Servlet和Portlet环境下注释处理器的方法。

E.1@RequestParam的属性

E.1.1、value

The name of the request parameter to bind to.

绑定的请求参数的名称。

@RequestParam(value="abc")等同于@RequestParam("abc")

E.1.2、required

Whether the parameter is required.

是否需要参数。

Default is true, leading to an exception thrown in case of the parameter missing in the request. Switch this to false if you prefer a null in case of the parameter missing.

默认为true,若请求中没有参数会导致抛出一个异常。若设置为false,若请求中没有参数就会返回null。

Alternatively, provide a defaultValue, which implicitly sets this flag to false.

在required=false时,最好设置一个defaultValue默认值。

@RequestParam(value = "abc",required=false)

E.1.3、defaultValue

The default value to use as a fallback. Supplying a default value implicitly sets required() to false.

当required=false时,设定默认值。

举例说明

@Controller

@RequestMapping("/a")

public class HelloWorldController {

@RequestMapping("/b")

public String helloWorld(Model model,@RequestParam("a") String abc) {

model.addAttribute("message", abc);

return "helloWorld";

}

}

F@ResponseBody

Annotation which indicates that a method return value should be bound to the web response body. Supported for annotated handler methods in Servlet environments.

这个注释它指示一个方法的返回值应该绑定到一个web响应的body中。它支持Servlet环境中的注释处理器方法。

应用@ResponseBody将会跳过视图处理,而是调用合适HttpMessageConverter,将返回值写入输出流。

举例说明

@Controller

@RequestMapping("/a")

public class HelloWorldController {

@RequestMapping("/b")

@ResponseBody

public String helloWorld() {

return "helloWorld";

}

}

或者这样定义

@Controller

public class HelloWorldController {

@RequestMapping("/a/b")

public @ResponseBody String helloWorld() {

return "helloWorld";

}

}

这时访问/a/b时,不是返回一个view名为helloWorld的视图,而是作出一个响应,其内容为helloWorld。

[@Controller]3 详解@CookieValue,@PathVariable,@RequestBody,@RequestHeader,@RequestParam的更多相关文章

  1. SpringMVC【开发Controller】详解

    前言 本文主要是讲解在Controller中的开发,主要的知识点有如下: 编码过滤器 使用注解开发 注解@RequestMapping详解 业务方法接收参数 字符串转日期 重定向和转发 返回JSON ...

  2. SpringMVC学习总结(三)——Controller接口详解(2)

    4.5.ServletForwardingController 将接收到的请求转发到一个命名的servlet,具体示例如下: package cn.javass.chapter4.web.servle ...

  3. Spring中@Component注解,@Controller注解详解

    在使用Spring的过程中,为了避免大量使用Bean注入的Xml配置文件,我们会采用Spring提供的自动扫描注入的方式,只需要添加几行自动注入的的配置,便可以完成 Service层,Controll ...

  4. Spring中@Component注解,@Controller注解详解

    在使用Spring的过程中,为了避免大量使用Bean注入的Xml配置文件,我们会采用Spring提供的自动扫描注入的方式,只需要添加几行自动注入的的配置,便可以完成 Service层,Controll ...

  5. Spring MVC 之@Controller@RequestMapping详解

    一:配置web.xml 1)问题:spring项目中有多个配置文件mvc.xml   dao.xml 2)解决:在web.xml中 <init-param> <param-name& ...

  6. SpringMVC学习总结(三)——Controller接口详解(1)

    4.12.ParameterizableViewController 参数化视图控制器,不进行功能处理(即静态视图),根据参数的逻辑视图名直接选择需要展示的视图. <bean name=&quo ...

  7. SpringMVC异常处理机制详解[附带源码分析]

    目录 前言 重要接口和类介绍 HandlerExceptionResolver接口 AbstractHandlerExceptionResolver抽象类 AbstractHandlerMethodE ...

  8. OkHttp3源码详解(一) Request类

    每一次网络请求都是一个Request,Request是对url,method,header,body的封装,也是对Http协议中请求行,请求头,实体内容的封装 public final class R ...

  9. SpringMVC @ModelAttribute 详解

    [@Controller]4 详解@ModelAttribute http://blog.sina.com.cn/s/blog_6d3c1ec601017q4p.html A.@ModelAttrib ...

随机推荐

  1. 玄机宝盒v1.6.1.1

    最新版本:玄机宝盒v1.6.1.1 玄机宝盒v1.6.1.1 04-14/2016 给你的将是无与伦比的体验http://bbs.msdn5.com/thread-15-1-1.html(出处: 玄机 ...

  2. 关于PHP Websocket 错误: "stream_select(): You MUST recompile PHP with a larger value of FD_SETSIZE" 的解决方案

    最近在使用Ratchet (一个PHP websocket框架)改造一个PHP网站的时候,出现了错误: "It is set to 1024, but you have descriptor ...

  3. 通过百度地图API实现搜索地址--第三方开源--百度地图(三)

    搜索地址功能是建立在能够通过百度地图API获取位置的基础上 通过百度地图定位获取位置详情:http://www.cnblogs.com/zzw1994/p/5008134.html package c ...

  4. Perl 关于 use strict 的用法

    什么场合要用 use strict 当你的程序有一定的行数时,尤其是在一页放不下时,或者是你找不到发生错误的原因时. 为什么要用 use strict? 众多的原因之一是帮你寻找因为错误拼写造成的错误 ...

  5. 表达式语言之ongl表达式

    OGNL的全称是Object Graph Navigation Language(对象图导航语言),它是一种强大的表达式语言,让你通过简单一致的表达式语法来读取和设置Java对象的属性值,调用对象的方 ...

  6. postgreSQL数据库(索引、视图)

    索引的含义与特点 索引是一个单独的.存储在磁盘上的数据库结构,它们包含对数据所有记录的引用指针,postgresql列类型都可以被索引,对相关列索引是提高查询操作效率的最佳途径.例如,查询select ...

  7. 【开发记录】iOS中使用 Reachability 检测网络

    如果你想在iOS程序中提供一仅在wifi网络下使用(Reeder),或者在没有网络状态下提供离线模式(Evernote).那么你会使用到Reachability来实现网络检测. 写本文的目的 了解Re ...

  8. left edge algorithm.

    今天在看GCPC 2013 的时候,遇到了一个简单的任务分配的题目. 即给定一系列任务[l,r], 每个人不能同时做两个, 最多需要几个人来完成? 这个问题的一个标准解法是对所有l,r排序,然后遇到l ...

  9. 【CocoaPods】配置CocoaPods前 - 本地安装好Ruby环境

    xcode (反正就是代码编辑器) Xcode就不用说了把. homebrew (反正就是软件管理器) homebrew是一个包管理器,用于在mac上安装一些os x上没有的UNiX工具(比如wget ...

  10. 微软职位内部推荐-Senior SDE for Big Data

    微软近期Open的职位: Title: Senior SDE The Big Data Tooling team looking for a talented and passionate devel ...