隨著Annotation的流行,一些主流框架都加入了對Annotation的支持。使用Annotation能夠簡化很多配置工作,能夠很大程度上提高程序開發的效率。本文將 Spring 2.5 新增的 Sping MVC 注解功能,介紹如何使用注解配置替換傳統的基于 XML 的 Spring MVC 配置。
棄用了struts,用spring mvc框架做了幾個項目,感覺都不錯,而且使用了注解方式,可以省掉一大堆配置文件。本文主要介紹使用注解方式配置的spring mvc。
@Controller
@Controller 負責注冊一個bean到spring上下文中,bean的ID默認為類名稱開頭字母小寫,你也可以自己指定,如下:
//方法一:
@Controller
public class TestController {}
//方法二:
@Controller("tmpController")
public class TestController {}
@RequestMapping
- @RequestMapping用來定義訪問的URL,你可以為整個類定義一個@RequestMapping,或者為每個方法指定一個。
把@RequestMapping放在類級別上,這可令它與方法級別上的@RequestMapping注解協同工作,取得縮小選擇范圍的效果。
例如:
@RequestMapping("/test")
public class TestController {}
該類下的所有訪問路徑都在/test之下。
- 將@RequestMapping用于整個類不是必須的,如果沒有配置,所有的方法的訪問路徑配置將是完全獨立的,沒有任何關聯。
- 完整的參數項為:@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 - value的配置還可以采用模版變量的形式,例如:
@RequestMapping(value="/owners/{ownerId}",method=RequestMethod.GET)這點將在介紹@PathVariable中詳細說明。 - @RequestMapping params的補充說明,你可以通過設置參數條件來限制訪問地址,例如params="myParam=myValue"表達式,訪問地址中參數只有包含了該規定的值"myParam=myValue"才能匹配得上,類似"myParam"之類的表達式也是支持的,表示當前請求的地址必須有該參數(參數的值可以是任意),"!myParam"之類的表達式表明當前請求的地址不能包含具體指定的參數"myParam"。
- 有一點需要注意的,如果為類定義了訪問地址為.do,.html之類的,則在方法級的@RequestMapping,不能再定義value值,否則會報錯,例如:
@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
- @PathVariable用于方法中的參數,表示方法參數綁定到地址URL的模板變量。
@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner( , Model model) {
Owner owner = ownerService.findOwner(ownerId);
model.addAttribute("owner", owner);
return "displayOwner";
}
- @PathVariable用于地址欄使用{xxx}模版變量時使用。如果@RequestMapping沒有定義類似"/{ownerId}" ,這種變量,則使用在方法中@PathVariable會報錯。
@ModelAttribute
- 應用于方法參數,參數可以在頁面直接獲取,相當于request.setAttribute(,)
- 應用于方法,將任何一個擁有返回值的方法標注上@ModelAttribute,使其返回值將會進入到模型對象的屬性列表中.
- 應用于方法參數時@ModelAttribute("xx"),須關聯到Object的數據類型,基本數據類型 如:int,String不起作用
@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
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 獲取請求的頭部信息
@ControllerAdvice 全局異常處理器 和@ExceptionHandler配合使用