(轉)Spring MVC的經常使用註解

@Controller(經常使用) html

註解一個類表示控制器,Spring MVC會自動掃描標註了這個註解的類。ajax

@RequestMapping(經常使用) spring

請求路徑映射,能夠標註類,也能夠是方法,能夠指定請求類型,默認不指定爲所有接收。其中有8個屬性:json

* value:指定請求的實際地址,指定的地址能夠是URI Template 模式。瀏覽器

* method:指定請求的method類型, GET、POST、PUT、DELETE等;  restful

  consumes: 指定處理請求的提交內容類型(Content-Type),例如application/json, text/html;  cookie

  produces:   指定返回的內容類型,僅當request請求頭中的(Accept)類型中包含該指定類型才返回;  app

  params: 指定request中必須包含某些參數值是,才讓該方法處理。  異步

  headers: 指定request中必須包含某些指定的header值,才能讓該方法處理請求。測試

@RequestParam

放在參數前,表示只能接收參數a=b格式的數據,即Content-Type爲 application/x-www-form-urlencoded類型的內容。 @RequestBody 放在參數前,表示參數從request body中獲取,而不是從地址欄獲取,因此這確定是接收一個POST請求的非a=b格式的數據,即Content-Type不爲 application/x-www-form-urlencoded類型的內容。

@ResponseBody(經常使用)

放在方法上或者返回類型前,表示此方法返回的數據放在response body裏面,而不是跳轉頁面。通常用於ajax請求,返回json數據。好比異步請求,但願響應的結果是json數據,那麼加上@Responsebody後,就會直接返回json數據。

@RestController

這個是Controller和ResponseBody的組合註解,表示@Controller標識的類裏面的全部返回參數都放在response body裏面。 @PathVariable 路徑綁定變量,用於綁定restful路徑上的變量。

@RequestHeader

放在方法參數前,用來獲取request header中的參數值。

@CookieValue

放在方法參數前,用來獲取request header cookie中的參數值。 GetMapping PostMapping PutMapping.. *Mapping的是Spring4.3加入的新註解,表示特定的請求類型路徑映射,而不須要寫RequestMethod來指定請求類型。

--------------------------------------------------------------------------------------------------------------------------------    

Controller控制器和@RequestMapping的做用

1、控制器定義
控制器提供訪問應用程序的行爲,一般經過服務接口定義或註解定義兩種方法實現。 控制器解析用戶的請求並將其轉換爲一個模型。在Spring MVC中一個控制器能夠包含多個Action(動做、方法)。

使用註解@Controller定義控制器
org.springframework.stereotype.Controller註解類型用於聲明Spring類的實例是一個控制器;

Spring可使用掃描機制來找到應用程序中全部基於註解的控制器類,爲了保證Spring能找到你的控制器,須要在配置文件中聲明組件掃描。

@Controller
@RequestMapping("/foo")
public class FooController {
 
//    @RequestMapping("/action1")
    @RequestMapping({"/","action1","111"})
    public String action1(Model model){
        model.addAttribute("message","測試action1");
        return "foo/index";
    }
}

2、@RequestMapping詳解
@RequestMapping註釋用於映射url到控制器類或一個特定的處理程序方法。可用於類或方法上。用於類上,表示類中的全部響應請求的方法都是以該地址做爲父路徑。該註解共有8個屬性,註解源碼以下:

/**
 * 用於映射url到控制器類或一個特定的處理程序方法.
 */
//該註解只能用於方法或類型上
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
 
    /**
     * 指定映射的名稱
     */
    String name() default "";
 
    /**
     * 指定請求的路徑映射,指定的地址能夠是uri模板,別名爲path
     */
    @AliasFor("path")
    String[] value() default {};
 
    /** 別名爲value,使用path更加形象
     * 只有用在一個Servlet環境:路徑映射URI(例如「/myPath.do」)。
     * Ant風格的路徑模式,同時也支持(例如,「/myPath/*.do」)。在方法層面,在主要的映射在類型級別表示相對路徑(例如,「edit.do」)
     * 的支持。路徑映射的URI可能包含佔位符(例如「/$ {}鏈接」)
     */
    @AliasFor("value")
    String[] path() default {};
 
    /**
     * 指定請求謂詞的類型如GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE. 收窄請求範圍 The
     * HTTP request methods to map to, narrowing the primary mapping: GET, POST,
     * HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE.
     */
    RequestMethod[] method() default {};
 
    /**
     * 映射請求的參數,收窄請求範圍 The parameters of the mapped request, narrowing the
     * primary mapping.
     */
    String[]params() default {};
 
    /**
     * 映射請求頭部,收窄請求範圍 The headers of the mapped request, narrowing the primary
     * mapping. RequestMapping(value = "/something", headers =
     * "content-type=text/*")
     */
    String[] headers() default {};
 
    /**
     * 指定處理請求的提交內容類型(Content-Type),例如application/json, text/html,收窄請求範圍 The
     * consumable media types of the mapped request, narrowing the primary
     * mapping.
     */
    String[] consumes() default {};
 
    /**
     * 指定返回的內容類型,僅當request請求頭中的(Accept)類型中包含該指定類型才返回 The producible media types
     * of the mapped request, narrowing the primary mapping. produces =
     * "text/plain" produces = {"text/plain", "application/*"} produces =
     * "application/json; charset=UTF-8"
     */
    String[] produces() default {};
}

value:指定請求的實際地址,指定的地址能夠是URI Template 模式(後面將會說明);

@RequestMapping({"/","action1","111"})
// 僅有value元素可省略
    public String action1(Model model){
        model.addAttribute("message","測試action1");
        return "foo/index";
    }


method:指定請求的method類型, GET、POST、PUT、DELETE等;

  @GetMapping("/action5")   //這是一個組合註解等同下面這個註解
//    @RequestMapping(value = "/action5",method = RequestMethod.GET)
    public String action5(Model model){
        model.addAttribute("message","請求謂語動詞是GET");
        return "foo/index";
    }

consumes: 指定處理請求的提交內容類型(Content-Type),例如application/json, text/html;

//  請求內容類型必須爲text/html
    @RequestMapping(value = "/action6",consumes = "!text/html")
    public String action6(Model model){
        model.addAttribute("message","請求的提交內容類型(Content-Type)是text/html");
        return "foo/index";
    }


produces:    指定返回的內容類型,僅當request請求頭中的(Accept)類型中包含該指定類型才返回;

// 客戶端接收json且編碼爲utf-8,多數瀏覽器Accept設置的爲*/*,接收任意類型
    @RequestMapping(value = "/action7",produces="application/json; charset=UTF-8")
    public String action7(Model model) {
        model.addAttribute("message", "客戶端能夠接收的類型是application/json; charset=UTF-8");
        return "foo/index";
    }


params: 指定request中必須包含某些參數值是,才讓該方法處理。

// 請求的參數必須包含id=215與name不等於abc
    @RequestMapping(value = "/action8",params={"id=215","name!=abc"})
    public String action8(Model model) {
        model.addAttribute("message", "請求的參數必須包含id=215與name不等於abc");
        return "foo/index";
    }


headers: 指定request中必須包含某些指定的header值,才能讓該方法處理請求。

// 請求頭部信息中必須包含Host=localhost:8080     @RequestMapping(value = "/action9",headers="Host=localhost:8080")     public String action9(Model model) {         model.addAttribute("message", "請求頭部信息中必須包含Host=localhost:8080");         return "foo/index";     }

相關文章
相關標籤/搜索