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

简介: [@Controller]3 详解@CookieValue,@PathVariable,@RequestBody,@RequestHeader,@RequestParam(2012-06-14 15:43:49)转载▼标签: cookievalue pathvariable requestbody requestheader分类: JavaSpring下列参数一般都和@RequestMapping配合使用。

下列参数一般都和@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。支持ServletPortlet环境。

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.

requiredfalse,请求中头丢失将返回这个值。

 

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请求的头信息。它支持ServletPortlet环境中的注释处理器方法。

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请求的参数。支持ServletPortlet环境下注释处理器的方法。

E.1@RequestParam的属性

E.1.1value

The name of the request parameter to bind to.

绑定的请求参数的名称。

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

E.1.2required

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.3defaultValue

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

 





目录
相关文章
|
2月前
|
JSON Java 数据格式
@RequestParam与@RequestBody使用对比
@RequestParam与@RequestBody使用对比
48 8
|
3月前
|
JSON 前端开发 数据格式
@RequestBody、@RequestParm、@PathVariable三个注解的区别
@RequestBody、@RequestParm、@PathVariable三个注解的区别
@RequestParam()和@PathVariable()的区别
@RequestParam()和@PathVariable()的区别
|
10月前
|
XML JSON 数据格式
@RequestParam @RequestBody @PathVariable 等参数绑定
@RequestParam @RequestBody @PathVariable 等参数绑定
|
4月前
|
Java API Spring
spring注解中@RequestParam和@PathVariable的区别
spring注解中@RequestParam和@PathVariable的区别
96 0
|
XML SQL JSON
@PathVariable、@RequestBody、@RequestParam、@ResponseBody、@Param的详解和用法
@PathVariable、@RequestBody、@RequestParam、@ResponseBody、@Param的详解和用法
147 0
|
Java API Spring
@RequestParam和@PathVariable的区别
@RequestParam注解获取URL中携带的请求参数的值既URL中“?”后携带的参数,传递参数的格式是:key=value;@PathVariable注解用于获取URL中路径的参数值,参数名由RequestMapping注解请求路径时指定,常用于restful风格的api中,传递参数格式:直接在url后添加需要传递的值即可
165 0
|
XML JSON Java
详解@RequestParam和@RequestBody
你好看官,里面请!今天笔者讲的是@RequestParam和@RequestBody。不懂或者觉得我写的有问题可以在评论区留言,我看到会及时回复。 注意:本文仅用于学习参考,不可用于商业用途,如需转载请跟我联系。
439 3
详解@RequestParam和@RequestBody
|
JSON 前端开发 数据格式
@RequestParam、@RequestBody、@PathVariable区别和案例分析
@RequestParam、@RequestBody、@PathVariable区别和案例分析
150 2