2013-02-06 23 views
0

我甲肝家庭控制器內我甲肝2層的方法之一是如何使用屬性值從一個方法到另一方法

@RequestMapping(value = "/mypage.te", method = RequestMethod.GET) 
    public String mypage1(Locale locale, Model model){ 

     Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 
     String name = auth.getName(); //get logged in username 
     model.addAttribute("username", name);  
     model.addAttribute("customGroup",grpDao.fetchCustomGroup());   
     model.addAttribute("serverTime", formattedDate); 
     model.addAttribute("username", name); 

     return "mypage"; 
} 

這裏在該方法實際上我把來自其執行一個道類grpDao.fetchCustomGroup()方法原生查詢並提取數據並返回,並保存在customGroup中。

現在同樣fetchcustomGroup()方法是另一種方法來使用,即

@RequestMapping(value = "/manageGrps.te", method = RequestMethod.GET) 
public String man_grp_connections(@RequestParam("id") Integer groupId,@RequestParam("name") String groupName, Model model) { 
    Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 
    String name = auth.getName(); 
    System.out.println("I am in the fetchCustomGroup controller"); 
    int profileid=grpDao.getProfileId(name);   
    //model.addAttribute("customGroup",grpDao.fetchCustomGroup()); 
    model.addAttribute("memberList",grpDao.fetchGroupMembers(groupId,profileid)); 
    model.addAttribute("groupid",groupId); 
    model.addAttribute("profileid",profileid); 

    model.addAttribute("groupName",groupName); 
    System.out.println("groupid="+groupId); 
    System.out.println("groupName="+groupName); 
    return "manageGrps"; 
} 

所以不是在這兩個我只是想在只有一個方法調用和使用結果的方法調用fetchCustomGroup()的兩個家庭控制器中的方法。

憑什麼我用customGroup在另一種方法使用的fetchCustomGroup()

+0

我只想複製第一個方法從數據庫中檢索到的相同數據 – amit

+0

我不想再次調用同一個方法 – amit

回答

0

我認爲,你想要的是避免執行查詢兩次結果。這可以通過不同的方式完成。最簡單的方法是將響應分配給控制器中的變量,然後使用getter而不是dao。控制器默認爲單身。喜歡的東西:

private Foo customGroup; 

private synchronized Foo getCustomGroup() { 
    if(customGroup == null) { 
     customGroup = grpDao.fetchCustomGroup(); 
    } 
    return customGroup; 
} 

然後使用getCustomGroup()代替grpDao.fetchCustomGroup()

我不知道你用的是什麼您的持久性,但使用的緩存也將避免執行查詢兩次不錯的主意。

相關問題