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

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

(2012-06-14 15:43:49)
下列參數一般都和@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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章