Spring MVC經常使用的註解

Spring MVC經常使用的註解

 

@Controller
@Controller 負責註冊一個bean 到spring 上下文中,bean 的ID 默認爲

類名稱開頭字母小寫,你也能夠本身指定,以下
方法一:
@Controller
public class TestController {}

方法二:
@Controller("tmpController")
public class TestController {}

@RequestMapping

1.@RequestMapping用來定義訪問的URL,你能夠爲整個類定義一個

@RequestMapping,或者爲每一個方法指定一個。
把@RequestMapping放在類級別上,這可令它與方法級別上的

@RequestMapping註解協同工做,取得縮小選擇範圍的效果。
例如:
@RequestMapping("/test")
public class TestController {}
則,該類下的全部訪問路徑都在/test之下。

2.將@RequestMapping用於整個類不是必須的,若是沒有配置,全部的方法

的訪問路徑配置將是徹底獨立的,沒有任何關聯。

3.完整的參數項爲:@RequestMapping(value="",method =

{"",""},headers={},params={"",""}),各參數說明以下:
value :String[] 設置訪問地址
method: RequestMethod[]設置訪問方式,字符數組,查看RequestMethod

類,包括GET, HEAD, POST, PUT, DELETE, OPTIONS, TRACE,經常使用

RequestMethod.GET,RequestMethod.POST
headers:String[] headers通常結合method = RequestMethod.POST使用
params: String[] 訪問參數設置,字符數組 例如:userId=id

4.value的配置還能夠採用模版變量的形式 ,例如:@RequestMapping

(value="/owners/{ownerId}", method=RequestMethod.GET),這點將在介

紹@PathVariable中詳細說明。

5.@RequestMapping params的補充說明,你能夠經過設置參數條件來限制

訪問地址,例如params="myParam=myValue"表達式,訪問地址中參數只有

包含了該規定的值"myParam=myValue"才能匹配得上,相似"myParam"之類

的表達式也是支持的,表示當前請求的地址必須有該參數(參數的值能夠是

任意),"!myParam"之類的表達式代表當前請求的地址不能包含具體指定的

參數"myParam"。

6.有一點須要注意的,若是爲類定義了訪問地址爲*.do,*.html之類的,則

在方法級的@RequestMapping,不能再定義value值,不然會報錯,例如
Java代碼
@RequestMapping("/bbs.do")
public class BbsController {
@RequestMapping(params = "method=getList")
public String getList() {
return "list";
}
@RequestMapping(value= "/spList")
public String getSpecialList() {
return "splist";
}
}

如上例:/bbs.do?method=getList 能夠訪問到方法getList() ;而訪

問/bbs.do/spList則會報錯.

@PathVariable
1.@PathVariable用於方法中的參數,表示方法參數綁定到地址URL的模板

變量。
例如:
Java代碼
@RequestMapping(value="/owners/{ownerId}",

method=RequestMethod.GET)
public String findOwner(@PathVariable String ownerId, Model

model) {
Owner owner = ownerService.findOwner(ownerId);
model.addAttribute("owner", owner);
return "displayOwner";
}

2.@PathVariable用於地址欄使用{xxx}模版變量時使用。
若是@RequestMapping沒有定義相似"/{ownerId}" ,這種變量,則使用在

方法中@PathVariable會報錯。


@ModelAttribute
1.應用於方法參數,參數能夠在頁面直接獲取,至關於

request.setAttribute(,)
2.應用於方法,將任何一個擁有返回值的方法標註上 @ModelAttribute,使

其返回值將會進入到模型對象的屬性列表中.
3.應用於方法參數時@ModelAttribute("xx"),須關聯到Object的數據類型

,基本數據類型 如:int,String不起做用
例如:
Java代碼
@ModelAttribute("items")//<——①向模型對象中添加一個名爲items的

屬性
public List<String> populateItems() {
List<String> lists = new ArrayList<String>();
lists.add("item1");
lists.add("item2");
return lists;
}
@RequestMapping(params = "method=listAllBoard")
public String listAllBoard(@ModelAttribute("currUser")User user,

ModelMap model) {
bbtForumService.getAllBoard();
//<——②在此訪問模型中的items屬性
System.out.println("model.items:" + ((List<String>)

model.get("items")).size());
return "listBoard";
}

在 ① 處,經過使用 @ModelAttribute 註解,populateItem() 方法將在

任何請求處理方法執行前調用,Spring MVC 會將該方法返回值以「items

」爲名放入到隱含的模型對象屬性列表中。
因此在 ② 處,咱們就能夠經過 ModelMap 入參訪問到 items 屬性,當執

行 listAllBoard() 請求處理方法時,② 處將在控制檯打印

出「model.items:2」的信息。固然咱們也能夠在請求的視圖中訪問到模型

對象中的 items 屬性。


@ResponseBody
這個註解能夠直接放在方法上,表示返回類型將會直接做爲HTTP響應字節

流輸出(不被放置在Model,也不被攔截爲視圖頁面名稱)。能夠用於ajax。

@RequestParam
@RequestParam是一個可選參數,例如:@RequestParam("id") 註解,因此

它將和URL所帶參數 id進行綁定
若是入參是基本數據類型(如 int、long、float 等),URL 請求參數中

必定要有對應的參數,不然將拋出

org.springframework.web.util.NestedServletException 異常,提示無

法將 null 轉換爲基本數據類型.

@RequestParam包含3個配置 @RequestParam(required = ,value="",

defaultValue = "")
required :參數是否必須,boolean類型,可選項,默認爲true
value: 傳遞的參數名稱,String類型,可選項,若是有值,對應到設置方

法的參數
defaultValue:String類型,參數沒有傳遞時爲參數默認指定的值

@SessionAttributes session管理
Spring 容許咱們有選擇地指定 ModelMap 中的哪些屬性須要轉存到

session 中,以便下一個請求屬對應的 ModelMap 的屬性列表中還能訪問

到這些屬性。這一功能是經過類定義處標註 @SessionAttributes 註解來

實現的。@SessionAttributes 只能聲明在類上,而不能聲明在方法上。

例如

@SessionAttributes("currUser") // 將ModelMap 中屬性名爲currUser 的屬性


@SessionAttributes({"attr1","attr2"})
@SessionAttributes(types = User.class)
@SessionAttributes(types = {User.class,Dept.class})
@SessionAttributes(types = {User.class,Dept.class},value={"attr1","attr2"})


@CookieValue 獲取cookie信息
@RequestHeader 獲取請求的頭部信息html

相關文章
相關標籤/搜索