亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    springmvc常用5種注解是哪些?

    springmvc常用5種注解是哪些?

    推薦教程:《java視頻教程》

    springmvc常用5種注解是哪些?

    springmvc常用5種注解為:

    一、組件型注解:

    1、@Component 在類定義之前添加@Component注解,他會被spring容器識別,并轉(zhuǎn)為bean。

    2、@Repository 對Dao實現(xiàn)類進行注解 (特殊的@Component)

    3、@Service 用于對業(yè)務(wù)邏輯層進行注解, (特殊的@Component)

    4、@Controller 用于控制層注解 , (特殊的@Component)

    以上四種注解都是注解在類上的,被注解的類將被spring初始話為一個bean,然后統(tǒng)一管理。

    二、請求和參數(shù)型注解:

    1、@RequestMapping:用于處理請求地址映射,可以作用于類和方法上。

    ●value:定義request請求的映射地址

    ●method:定義地request址請求的方式,包括【GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE.】默認接受get請求,如果請求方式和定義的方式不一樣則請求無法成功。

    ●params:定義request請求中必須包含的參數(shù)值。

    ●headers:定義request請求中必須包含某些指定的請求頭,如:RequestMapping(value = "/something", headers = "content-type=text/*")說明請求中必須要包含"text/html", "text/plain"這中類型的Content-type頭,才是一個匹配的請求。

    ●consumes:定義請求提交內(nèi)容的類型。

    ●produces:指定返回的內(nèi)容類型,僅當(dāng)request請求頭中的(Accept)類型中包含該指定類型才返回

    @RequestMapping(value="/requestTest.do",params = {"name=sdf"},headers = {"Accept-Encoding=gzip, deflate, br"},method = RequestMethod.GET)      public String getIndex(){        System.out.println("請求成功");        return "index";      }

    上面代碼表示請求的方式為GET請求,請求參數(shù)必須包含name=sdf這一參數(shù),然后請求頭中必須有 Accept-Encoding=gzip, deflate, br這個類型頭。

    這樣通過注解就能對一個請求進行約束了。

    2.@RequestParam:用于獲取傳入?yún)?shù)的值

    ●value:參數(shù)的名稱

    ●required:定義該傳入?yún)?shù)是否必須,默認為true,(和@RequestMapping的params屬性有點類似)

    @RequestMapping("/requestParams1.do")     public String requestParams1(@RequestParam(required = false) String name){       System.out.println("name = "+name);       return "index";     }     @RequestMapping("/requestParams2.do")     public String requestParams2(@RequestParam(value = "name",required = false) String names){       System.out.println("name = "+names);       return "index";     }

    兩種請入?yún)⒎绞绞且粯拥模@示聲明value的名稱時,入?yún)?shù)名和value一樣,沒有顯示聲明的話,像第一種方式聲明的,入?yún)?shù)名和函數(shù)參數(shù)變量名一樣。

    3.@PathViriable:用于定義路徑參數(shù)值

    ●value:參數(shù)的名稱

    ●required:定義傳入?yún)?shù)是否為必須值

    @RequestMapping("/{myname}/pathVariable2.do")  public String pathVariable2(@PathVariable(value = "myname") String name){       System.out.println("myname = "+name);    return "index";     }

    這個路徑聲明了{myname}作為路徑參數(shù),那么這一段路徑將為任意值,@PathVariable將可以根據(jù)value獲取路徑的值。

    4.@ResponseBody:作用于方法上,可以將整個返回結(jié)果以某種格式返回,如json或xml格式。

    @RequestMapping("/{myname}/pathVariable2.do")      @ResponseBody      public String pathVariable2(@PathVariable(value = "myname") String name){        System.out.println("myname = "+name);        return "index";      }

    它返回的不是一個頁面,而是把字符串“index”直接在頁面打印出來了,這其實和如下代碼時類似的。

    PrintWriter out = resp.getWriter();    out.print("index");    out.flush();

    5、@CookieValue:用于獲取請求的Cookie值

    @RequestMapping("/requestParams.do")      public String requestParams(@CookieValue("JSESSIONID") String cookie){        return "index";      }


    6、@ModelAttribute:

    用于把參數(shù)保存到model中,可以注解方法或參數(shù),注解在方法上的時候,該方法將在處理器方法執(zhí)行之前執(zhí)行,然后把返回的對象存放在 session(前提時要有@SessionAttributes注解) 或模型屬性中,@ModelAttribute(“attributeName”) 在標(biāo)記方法的時候指定,若未指定,則使用返回類型的類名稱(首字母小寫)作為屬性名稱?!?/p>

    @ModelAttribute("user")     public UserEntity getUser(){       UserEntity userEntityr = new UserEntity();       userEntityr.setUsername("asdf");       return userEntityr;     }          @RequestMapping("/modelTest.do")     public String getUsers(@ModelAttribute("user") UserEntity user){       System.out.println(user.getUsername());       return "/index";     }

    如上代碼中,使用了@ModelAttribute("user")注解,在執(zhí)行控制器前執(zhí)行,然后將生成一個名稱為user的model數(shù)據(jù),在控制器中我們通過注解在參數(shù)上的@ModelAttribute獲取參數(shù),然后將model應(yīng)用到控制器中,在jsp頁面中我們同樣可以使用它,

    <body>   ${user.username} </body>

    7、@SessionAttributes

    默認情況下Spring MVC將模型中的數(shù)據(jù)存儲到request域中。當(dāng)一個請求結(jié)束后,數(shù)據(jù)就失效了。如果要跨頁面使用。那么需要使用到session。而@SessionAttributes注解就可以使得模型中的數(shù)據(jù)存儲一份到session域中。配合@ModelAttribute("user")使用的時候,會將對應(yīng)的名稱的model值存到session中,

    @Controller   @RequestMapping("/test")   @SessionAttributes(value = {"user","test1"})   public class LoginController{     @ModelAttribute("user")     public UserEntity getUser(){       UserEntity userEntityr = new UserEntity();       userEntityr.setUsername("asdf");       return userEntityr;     }          @RequestMapping("/modelTest.do")     public String getUsers(@ModelAttribute("user") UserEntity user ,HttpSession session){       System.out.println(user.getUsername());       System.out.println(session.getAttribute("user"));       return "/index";     }   }

    結(jié)合上一個例子的代碼,加了@SessionAttributes注解,然后請求了兩次,第一次session中不存在屬性名為user的值,第二次請求的時候發(fā)現(xiàn)session中又有了,這是因為,這是因為第一次請求時,model數(shù)據(jù)還未保存到session中請求結(jié)束返回的時候才保存,在第二次請求的時候已經(jīng)可以獲取上一次的model了

    注意:@ModelAttribute("user") UserEntity user獲取注解內(nèi)容的時候,會先查詢session中是否有對應(yīng)的屬性值,沒有才去查詢Model。

    推薦相關(guān)文章:《java開發(fā)教程》

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號