Õâ´Îmuyu¸ø´ó¼Ò·ÖÏíһϣ¬ÔÚSpringMVCÖУ¬×î³£Óõļ¸¸ö×¢½âµÄʹÓ÷½Ê½ÓëһЩ˵Ã÷£¬·½±ã´ó¼Ò¶ÔSpringMVC×¢½âµÄÓ¦ÓúÍѧϰ¡£
@Controller
@Controller ¸ºÔð×¢²áÒ»¸öbeanµ½springÉÏÏÂÎÄÖУ¬beanµÄIDĬÈÏΪÀàÃû³Æ¿ªÍ·×ÖĸСд,ÄãÒ²¿ÉÒÔ×Ô¼ºÖ¸¶¨£¬ÈçÏÂ
·½·¨Ò»£º
@Controller
public classTestController {}
·½·¨¶þ£º
@Controller("tmpController")
public classTestController {}
@RequestMapping
1.@RequestMappingÓÃÀ´¶¨Òå·ÃÎʵÄURL£¬Äã¿ÉÒÔΪÕû¸öÀඨÒåÒ»¸ö
@RequestMapping£¬»òÕßΪÿ¸ö·½·¨Ö¸¶¨Ò»¸ö¡£
°Ñ@RequestMapping·ÅÔÚÀ༶±ðÉÏ£¬Õâ¿ÉÁîËüÓë·½·¨¼¶±ðÉϵÄ
@RequestMapping×¢½âÐͬ¹¤×÷£¬È¡µÃËõСѡÔñ·¶Î§µÄЧ¹û¡£
ÀýÈ磺
@RequestMapping("/test")
public classTestController{}
Ôò£¬¸ÃÀàϵÄËùÓзÃÎÊ·¾¶¶¼ÔÚ/test֮ϡ£
2.½«@RequestMappingÓÃÓÚÕû¸öÀ಻ÊDZØÐëµÄ£¬Èç¹ûûÓÐÅäÖã¬ËùÓеķ½·¨µÄ·ÃÎÊ·¾¶ÅäÖý«ÊÇÍêÈ«¶ÀÁ¢µÄ£¬Ã»ÓÐÈκιØÁª¡£
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.@RequestMappingparamsµÄ²¹³ä˵Ã÷£¬Äã¿ÉÒÔͨ¹ýÉèÖòÎÊýÌõ¼þÀ´ÏÞÖÆ·ÃÎʵØÖ·£¬ÀýÈç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 StringgetSpecialList() {
return "splist";
}
}
ÈçÉÏÀý£º/bbs.do?method=getList¿ÉÒÔ·ÃÎʵ½·½·¨getList() £»¶ø·ÃÎÊ/bbs.do/spListÔò»á±¨´í.
@PathVariable
1.@PathVariableÓÃÓÚ·½·¨ÖеIJÎÊý£¬±íʾ·½·¨²ÎÊý°ó¶¨µ½µØÖ·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 = newArrayList<String>();
lists.add("item1");
lists.add("item2");
return lists;
}
@RequestMapping(params= "method=listAllBoard")
public String listAllBoard(@ModelAttribute("currUser")Useruser,ModelMap model){
bbtForumService.getAllBoard();
//<¡ª¡ª¢ÚÔÚ´Ë·ÃÎÊÄ£ÐÍÖеÄitemsÊôÐÔ
System.out.println("model.items:" + ((List<String>)
model.get("items")).size());
return "listBoard";
}
ÔÚ¢Ù´¦£¬Í¨¹ýʹÓÃ@ModelAttribute×¢½â£¬populateItem()·½·¨½«ÔÚÈκÎÇëÇó´¦Àí·½·¨Ö´ÐÐÇ°µ÷Óã¬SpringMVC »á½«¸Ã·½·¨·µ»ØÖµÒÔ¡°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"})
@Cookieva lue »ñÈ¡cookieÐÅÏ¢
@RequestHeader »ñÈ¡ÇëÇóµÄÍ·²¿ÐÅÏ¢