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 獲取請求的頭部信息
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章