[@Controller]3 detailed @cookievalue, @PathVariable, @RequestBody, @RequestHeader, @RequestParam

Source: Internet
Author: User
Tags http cookie

[@Controller]3 detailed @cookievalue, @PathVariable, @RequestBody, @RequestHeader, @RequestParam reprint: http://blog.sina.com.cn /s/blog_6d3c1ec601017q4l.html The following parameters are generally used in conjunction with @requestmapping.

A , @ Cookievalue

Org.springframework.web.bind.annotation.CookieValue

Public @interface Cookievalue

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

This comment indicates that a method parameter is bound to an HTTP cookie. Supports servlet and portlet environments.

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

The parameters of this method can be declared as a cookie type or string, int, and so on.

A.1 , @ Cookievalue of the Properties

String value

The name of the cookie to bind to.

The cookie name of the binding.

Boolean Required

Whether the header is required.

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

If the head is needed. The default is true, and a missing header in the request throws an exception. False, the missing header in the request will return NULL.

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

Therefore, provide a defaultvalue.

String DefaultValue

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

When required is false, the missing header in the request returns this value.

B , @ pathvariable

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

This parameter indicates that a parameter of the method is bound to a URI template variable. The processor method that is @requestmapping commented in the servlet environment.

B.1 , @PathVariable of the Properties

Value

The URI template variable to bind to.

The binding URI template variable.

Examples Show

@Controller

Public class Helloworldcontroller {@RequestMapping ("/helloworld/{userid}")

Public String HelloWorld (Modelmap model, @PathVariable ("userid") string userid) {

Model.addattribute ("AttributeName", userId);

return "HelloWorld";

}

}

When the URI template variable and the parameter name of the method are the same, the definition of value can be omitted, @PathVariable achieve the same effect.

C , @RequestBody

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

This annotation indicates that a method parameter is bound to the body of a Web request. It supports annotation processor methods in a servlet environment.

Examples Show

@Controller

Public class Helloworldcontroller {

@RequestMapping ("/hello.do")

Public String HelloWorld (model model, @RequestBody string reqbody) {

Model.addattribute ("message", reqbody);

return "HelloWorld";

}

}

The value of this parameter reqbody is all the values of the form form of the requested page.

D , @ requestheader

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

This annotation indicates that a method's parameters are bound to the header information of a Web request. It supports annotation processor methods in servlet and portlet environments.

D.1 , @ requestheader of the Properties

String DefaultValue

The default value to use as a fallback.

The default return value.

Boolean Required

Whether the header is required.

If the header is required.

String value

The name of the request header to bind to.

The name of the request header for the binding.

Examples Show

@Controller

Public class Helloworldcontroller {

@RequestMapping ("/hello.do")

Public String HelloWorld (model model, @RequestHeader ("Accept") string info) {

Model.addattribute ("message", info);

return "HelloWorld";

}

}

This parameter info will get the requested accept header information.

E , @ Requestparam

Org.springframework.web.bind.annotation.RequestParam

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

This parameter indicates that the parameters of a method should be bound to the parameters of a Web request. A method that supports annotation processors in servlet and portlet environments.

E.1 , @RequestParam of the Properties

e.1.1 , Value

The name of the request parameter to bind to.

The name of the binding request parameter.

@RequestParam (value= "abc") is equivalent to @requestparam ("abc")

e.1.2 , Required

Whether the parameter is required.

Whether parameters are required.

Default is true, leading to a 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.

The default is true, which causes an exception to be thrown if there are no parameters in the request. If set to False, NULL is returned if there are no parameters in the request.

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

When Required=false, it is best to set a DefaultValue default value.

@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.

When Required=false, set the default value.

Examples Show

@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 is bound to the Web response body. Supported for annotated handler methods in Servlet environments.

This annotation indicates that the return value of a method should be bound to the body of a web response. It supports annotation processor methods in a servlet environment.

Applying @Responsebody will skip the view processing, instead call the appropriate httpmessageconverter and write the return value to the output stream.

Examples Show

@Controller

@RequestMapping ("/A")

Public class Helloworldcontroller {

@RequestMapping ("/b")

@ResponseBody

Public String HelloWorld () {

return "HelloWorld";

}

}

or so define

@Controller

Public class Helloworldcontroller {

@RequestMapping ("/a/b")

public @ResponseBody String HelloWorld () {

return "HelloWorld";

}

}

When accessing/a/b, instead of returning a view named HelloWorld, a response is made, and the content is HelloWorld.

[@Controller]3 detailed @cookievalue, @PathVariable, @RequestBody, @RequestHeader, @RequestParam

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.