springMVC的各種注解的使用及說(shuō)明
文章出處:http://psychicreadingswithdeb.com 作者:興邦開(kāi)發(fā)部 人氣: 發(fā)表時(shí)間:2015年12月28日
springMVC基于注解的配置類(lèi)型有很多,這里介紹幾個(gè)常用的
通過(guò)@controller標注即可將class定義為一個(gè)controller類(lèi)。
參數說(shuō)明:value 表示需要匹配的url的格式。
method 表示所需處理請求的http 協(xié)議(如get,post,put,delete等),可選值為RequestMethod這個(gè)enum的值。
params 格式為”paramname=paramvalue” 或 “paramname!=paramvalue”。 表示參數必須等于某值,或者不等于才進(jìn)入此映射方法。不填寫(xiě)的時(shí)候表明不限制
headers 用來(lái)限定對應的reqeust請求的headers中必須包括的內容,例如headers={"Connection=keep-alive"}, 表示請求頭中的connection的值必須為keep-alive。
參數說(shuō)明:
value 對應表單name空間的值
required 是否允許為空
defaultValue 默認值
@PathVariable
獲得地址欄中傳的參數 例如:
[java] view plaincopyprint?
@RequestMapping(value="/{groupId}.do")
public void detail(@PathVariable long groupId){
groupRepository.selectOne(groupId);
}
4、 @SessionAttributes
寫(xiě)在類(lèi)級別的注解,定義一個(gè)session attributes,屬性名字為SessionAttributes指定??梢灾付ǘ鄠€(gè)(數組),也同時(shí)可以指定類(lèi)型。例如:
[java] view plaincopyprint?
@Controller
@SessionAttributes( { "user" })
@RequestMapping("/test")
public class ControllerTest {
@RequestMapping("/session")
@ResponseBody
public String sessionIn(@ModelAttribute("user") User user) {
return "index";
}
}