2011-03-08 96 views
1

嘿夥計們。我試圖在HttpSessionListener中獲得會話bean,以便當用戶註銷或會話過期時,我可以刪除用戶在應用程序中創建的一些文件。我猜測會話bean不存在,因爲會話被銷燬。我希望仍然可以刪除這些文件。謝謝您的幫助。從HttpSessionListener獲取SessionScoped bean?

@WebListener 
    public class SessionListener implements HttpSessionListener { 

     @Override 
     public void sessionCreated(HttpSessionEvent se) { 
      HttpSession session = se.getSession(); 
      System.out.print(getTime() + " (session) Created:"); 
      System.out.println("ID=" + session.getId() + " MaxInactiveInterval=" 
        + session.getMaxInactiveInterval()); 
     } 

     @Override 
     public void sessionDestroyed(HttpSessionEvent se) { 
      HttpSession session = se.getSession(); 

      FacesContext context = FacesContext.getCurrentInstance(); 
      //UserSessionBean userSessionBean = (UserSessionBean) context.getApplication().evaluateExpressionGet(context, "#{userSessionBean}", UserSessionBean.class) 
      UserSessionBean userSessionBean = (UserSessionBean) session.getAttribute("userSessionBean"); 

      System.out.println(session.getId()); 
      System.out.println("File size :" + userSessionBean.getFileList().size()); 

      for (File file : userSessionBean.getFileList()) { 
       file.delete(); 
      } 
     } 
    } 

To BalusC:我回到了你以前想到的方法。在我的應用程序中將字節流式傳輸給用戶並不靈活。我發現我需要在頁面上的ajax中做很多事情,如果我必須發送非ajax請求來流式傳輸要下載的文件,那麼這是不可能的。這種方式通過ajax調用(生成文檔)完成繁重的工作,快速簡單的工作可以通過非Ajax調用完成。

@ManagedBean(name = "userSessionBean") 
@SessionScoped 
public class UserSessionBean implements Serializable, HttpSessionBindingListener { 

    final Logger logger = LoggerFactory.getLogger(UserSessionBean.class); 
    @Inject 
    private User currentUser; 
    @EJB 
    UserService userService; 
    private List<File> fileList; 

    public UserSessionBean() { 

     fileList = new ArrayList<File>(); 
    } 

    @PostConstruct 
    public void onLoad() { 

     Principal principal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal(); 
     String email = principal.getName(); 

     if (email != null) { 
      currentUser = userService.findUserbyEmail(email); 
     } else { 

      logger.error("Couldn't find user information from login!"); 
     } 
    } 

    public User getCurrentUser() { 
     return currentUser; 
    } 

    public void setCurrentUser(User currentUser) { 
     this.currentUser = currentUser; 
    } 

    public List<File> getFileList() { 
     return fileList; 
    } 

    @Override 
    public void valueUnbound(HttpSessionBindingEvent event) { 

     logger.info("UserSession unbound"); 
     logger.info(String.valueOf(fileList.size())); 
     for (File file : fileList) { 
      logger.info(file.getName()); 
      file.delete(); 
     } 
    } 

    @Override 
    public void valueBound(HttpSessionBindingEvent event) { 
     logger.info("UserSessionBean bound"); 
    } 
} 

回答

3

此代碼應該正常工作。請注意,FacesContext不一定在那裏可用,因爲在該點運行的線程中不一定需要HTTP請求,但您已經對此進行了評估。你確定你是實際上運行代碼如問題所示?清理,重建,重新部署等。

另一種方法是讓您的UserSessionBean實施HttpSessionBindingListener,然後在valueUnbound()方法中執行作業。

@ManagedBean 
@SessionScoped 
public class UserSessionBean implements HttpSessionBindingListener { 

    @Override 
    public void valueUnbound(HttpSessionBindingEvent event) { 
     for (File file : files) { 
      file.delete(); 
     } 
    } 

    // ... 
} 
+0

嘿BalusC我也試過。我知道文件列表中有文件對象。我可以通過在我的應用程序中記錄大小來測試。但是,當調用unbound時,文件對象大小爲0.這與我在會話偵聽器中遇到的問題相同。我已經使用代碼更新了我的原始帖子。我100%確定該列表正在獲取對象。 – 2011-03-09 14:22:04

+0

你不只是填寫/調試/訪問錯誤的列表? – BalusC 2011-03-09 14:23:23

+0

不是我所知道的。該列表是userSessionBean對象的一部分。我可以在應用程序中檢索並添加到列表中。 – 2011-03-09 15:29:19

相關問題