sPRing mvc常用的注解:
個(gè)介紹。
@Controller
@Controller 負(fù)責(zé)注冊(cè)一個(gè)bean 到spring 上下文中,bean 的ID 默認(rèn)為
類名稱開頭字母小寫,你也可以自己指定,如下
方法一:
@Controller
public class TestController {}
方法二:
@Controller("tmpController")
public class TestController {}
@RequestMapping
1.@RequestMapping用來定義訪問的URL,你可以為整個(gè)類定義一個(gè)
@RequestMapping,或者為每個(gè)方法指定一個(gè)。
把@RequestMapping放在類級(jí)別上,這可令它與方法級(jí)別上的
@RequestMapping注解協(xié)同工作,取得縮小選擇范圍的效果。
例如:
@RequestMapping("/test")
public class TestController {}
則,該類下的所有訪問路徑都在/test之下。
2.將@RequestMapping用于整個(gè)類不是必須的,如果沒有配置,所有的方法
的訪問路徑配置將是完全獨(dú)立的,沒有任何關(guān)聯(lián)。
3.完整的參數(shù)項(xiàng)為:@RequestMapping(value="",method =
{"",""},headers={},params={"",""}),各參數(shù)說明如下:
value :String[] 設(shè)置訪問地址
method: RequestMethod[]設(shè)置訪問方式,字符數(shù)組,查看RequestMethod
類,包括GET, HEAD, POST, PUT, DELETE, OPTIONS, TRACE,常用
RequestMethod.GET,RequestMethod.POST
headers:String[] headers一般結(jié)合method = RequestMethod.POST使用
params: String[] 訪問參數(shù)設(shè)置,字符數(shù)組 例如:userId=id
4.value的配置還可以采用模版變量的形式 ,例如:@RequestMapping
(value="/owners/{ownerId}", method=RequestMethod.GET),這點(diǎn)將在介
紹@PathVariable中詳細(xì)說明。
5.@RequestMapping params的補(bǔ)充說明,你可以通過設(shè)置參數(shù)條件來限制
訪問地址,例如params="myParam=myValue"表達(dá)式,訪問地址中參數(shù)只有
包含了該規(guī)定的值"myParam=myValue"才能匹配得上,類似"myParam"之類
的表達(dá)式也是支持的,表示當(dāng)前請(qǐng)求的地址必須有該參數(shù)(參數(shù)的值可以是
任意),"!myParam"之類的表達(dá)式表明當(dāng)前請(qǐng)求的地址不能包含具體指定的
參數(shù)"myParam"。
6.有一點(diǎn)需要注意的,如果為類定義了訪問地址為*.do,*.html之類的,則
在方法級(jí)的@RequestMapping,不能再定義value值,否則會(huì)報(bào)錯(cuò),例如
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則會(huì)報(bào)錯(cuò).
@PathVariable
1.@PathVariable用于方法中的參數(shù),表示方法參數(shù)綁定到地址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}模版變量時(shí)使用。
如果@RequestMapping沒有定義類似"/{ownerId}" ,這種變量,則使用在
方法中@PathVariable會(huì)報(bào)錯(cuò)。
@ModelAttribute
1.應(yīng)用于方法參數(shù),參數(shù)可以在頁面直接獲取,相當(dāng)于
request.setAttribute(,)
2.應(yīng)用于方法,將任何一個(gè)擁有返回值的方法標(biāo)注上 @ModelAttribute,使
其返回值將會(huì)進(jìn)入到模型對(duì)象的屬性列表中.
3.應(yīng)用于方法參數(shù)時(shí)@ModelAttribute("xx"),須關(guān)聯(lián)到Object的數(shù)據(jù)類型
,基本數(shù)據(jù)類型 如:int,String不起作用
例如:
Java代碼
@ModelAttribute("items")//<——①向模型對(duì)象中添加一個(gè)名為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() 方法將在
任何請(qǐng)求處理方法執(zhí)行前調(diào)用,Spring MVC 會(huì)將該方法返回值以“items
”為名放入到隱含的模型對(duì)象屬性列表中。
所以在 ② 處,我們就可以通過 ModelMap 入?yún)⒃L問到 items 屬性,當(dāng)執(zhí)
行 listAllBoard() 請(qǐng)求處理方法時(shí),② 處將在控制臺(tái)打印
出“model.items:2”的信息。當(dāng)然我們也可以在請(qǐng)求的視圖中訪問到模型
對(duì)象中的 items 屬性。
@ResponseBody
這個(gè)注解可以直接放在方法上,表示返回類型將會(huì)直接作為HTTP響應(yīng)字節(jié)
流輸出(不被放置在Model,也不被攔截為視圖頁面名稱)。可以用于Ajax。
@RequestParam
@RequestParam是一個(gè)可選參數(shù),例如:@RequestParam("id") 注解,所以
它將和URL所帶參數(shù) id進(jìn)行綁定
如果入?yún)⑹腔緮?shù)據(jù)類型(如 int、long、float 等),URL 請(qǐng)求參數(shù)中
一定要有對(duì)應(yīng)的參數(shù),否則將拋出
org.springframework.web.util.NestedServletException 異常,提示無
法將 null 轉(zhuǎn)換為基本數(shù)據(jù)類型.
@RequestParam包含3個(gè)配置 @RequestParam(required = ,value="",
defaultValue = "")
required :參數(shù)是否必須,boolean類型,可選項(xiàng),默認(rèn)為true
value: 傳遞的參數(shù)名稱,String類型,可選項(xiàng),如果有值,對(duì)應(yīng)到設(shè)置方
法的參數(shù)
defaultValue:String類型,參數(shù)沒有傳遞時(shí)為參數(shù)默認(rèn)指定的值
@sessionAttributes session管理
Spring 允許我們有選擇地指定 ModelMap 中的哪些屬性需要轉(zhuǎn)存到
session 中,以便下一個(gè)請(qǐng)求屬對(duì)應(yīng)的 ModelMap 的屬性列表中還能訪問
到這些屬性。這一功能是通過類定義處標(biāo)注 @SessionAttributes 注解來
實(shí)現(xiàn)的。@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 獲取請(qǐng)求的頭部信息
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注