2011-03-08 38 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