Spring MVC中常用注解之@SessionAttributes @ModelAttribute詳解


@RequestMapping(value = "/doupdate") public String doupdate(@RequestParam(value = "id") String id, Model model) {  model.addAttribute("person", personService.getPersonById(id)); return "editpage"; }

      spring 2.0 定義了一個 org.springframework.ui.ModelMap 類,它作為通用的模型數據承載對象,傳遞數據供視圖所用。我們可以在請求處理方法中聲明一個 ModelMap 類型的入參,Spring 會將本次請求模型對象引用通過該入參傳遞進來,這樣就可以在請求處理方法內部訪問模型對象了。

     上面Controller中的模型數據傳遞至editpage.jsp之后如下所示:

<form id="saveForm" action="${pageContext.request.contextPath}/person/updateperson" method="post">
        <input type="hidden" name="id" value="${person.id}" />
        <table style="font-size: :16px">
            <tr>
                <td>姓名:</td>
                <td><input type="text" value="${person.name}" name="name" /></td>
            </tr>
            <tr>
                <td align="right">
                <input type="submit" value="更新" /> &nbsp;&nbsp;<a href="javascript:history.go(-1)">退回 </a>
            </tr>
        </table>
    </form>

      對於當次請求所對應的模型對象來說,其所有屬性都將存放到 request 的屬性列表中。象上面的例子,ModelMap 中的 person屬性將放到 request 的屬性列表中,所以可以在 JSP 視圖頁面中通過 request.getAttribute(“person”) 或者通過 ${person} EL 表達式訪問模型對象中的person對象,通過 ${person.name} 得到模型對象中的person對象的name屬性。從這個角度上看, ModelMap 相當於是一個向 request 屬性列表中添加對象的一條管道,借由 ModelMap 對象的支持,我們可以在一個不依賴 Servlet API 的 Controller 中向 request 中添加屬性。 
在默認情況下,ModelMap 中的屬性作用域是 request 級別是,也就是說,當本次請求結束后,ModelMap 中的屬性將銷毀。如果希望在多個請求中共享 ModelMap 中的屬性,必須將其屬性轉存到 session 中,這樣 ModelMap 的屬性才可以被跨請求訪問。 
Spring 允許我們有選擇地指定 ModelMap 中的哪些屬性需要轉存到 session 中,以便下一個請求屬對應的 ModelMap 的屬性列表中還能訪問到這些屬性。這一功能是通過類定義處標注 @SessionAttributes 注解來實現的。請看下面的代碼: 

package com.baobaotao.web; … import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.SessionAttributes; @Controller @RequestMapping("/bbtForum.do") @SessionAttributes("currUser") //①將ModelMap中屬性名為currUser的屬性 //放到Session屬性列表中,以便這個屬性可以跨請求訪問 
public class BbtForumController { … @RequestMapping(params = "method=listBoardTopic") public String listBoardTopic(@RequestParam("id")int topicId, User user, ModelMap model) { bbtForumService.getBoardTopics(topicId); System.out.println("topicId:" + topicId); System.out.println("user:" + user); model.addAttribute("currUser",user); //②向ModelMap中添加一個屬性 
        return "listTopic"; } } 

 我們在 ② 處添加了一個 ModelMap 屬性,其屬性名為 currUser,而 ① 處通過 @SessionAttributes 注解將 ModelMap 中名為 currUser 的屬性放置到 Session 中,所以我們不但可以在 listBoardTopic() 請求所對應的 JSP 視圖頁面中通過 request.getAttribute(“currUser”) 和 session.getAttribute(“currUser”) 獲取 user 對象,還可以在下一個請求所對應的 JSP 視圖頁面中通過 session.getAttribute(“currUser”) 訪問到這個屬性。 
這里我們僅將一個 ModelMap 的屬性放入 Session 中,其實 @SessionAttributes 允許指定多個屬性。你可以通過字符串數組的方式指定多個屬性,如 @SessionAttributes({“attr1”,”attr2”})。此外,@SessionAttributes 還可以通過屬性類型指定要 session 化的 ModelMap 屬性,如 @SessionAttributes(types = User.class),當然也可以指定多個類,如 @SessionAttributes(types = {User.class,Dept.class}),還可以聯合使用屬性名和屬性類型指定:@SessionAttributes(types = {User.class,Dept.class},value={“attr1”,”attr2”})。 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM