从HttpSessionListener获取SessionScoped bean? [英] Getting SessionScoped bean from HttpSessionListener?

查看:95
本文介绍了从HttpSessionListener获取SessionScoped bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.我正在尝试在HttpSessionListener中获取会话bean,以便在用户注销或会话过期时可以删除用户在应用程序中创建的一些文件.我猜测会话bean不存在,因为会话已被破坏.我希望仍然以某种方式删除这些文件.感谢您的帮助.

Hey guys. I'm trying to get a session bean in a HttpSessionListener so that when the user logs out or the session expires I can delete some files that the user created in the application. I'm guessing the session bean doesn't exist because the session is destroyed. I was hoping to still delete these files some how. Thanks for the help.

   @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();
            }
        } 
    }

致BalusC:我又回到了您以前想到的方法.在我的应用程序中,将字节流传输给用户并不灵活.我发现我需要在页面上的ajax中做很多事情,如果我必须发送一个非ajax请求以流式传输要下载的文件,那是不可能的.这样,繁重的工作就可以通过ajax调用(生成文档)完成,而快速简便的工作可以通过非ajax调用完成.

To BalusC: I'm back to the method you previously had in mind. Streaming the bytes to the user wasn't flexible in my application. I found that I needed to do a lot of things in ajax on the page that was not possible if I had to send a non ajax request to stream a file to be downloaded. This way the heavy lifting is done with an ajax call(generating the document) and the fast and easy work is can be done with a non ajax call.

   @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");
    }
}

推荐答案

此代码应该可以正常工作.请注意,FacesContext不一定在那里可用,因为此时并不一定要在线程中运行HTTP请求,但是您已经对此表示反对.您确定您确实在运行 问题中显示的代码吗?清理,重建,重新部署等.

This code should work fine. Note that the FacesContext isn't necessarily available there since there's not necessarily means of a HTTP request in the thread running at that point, but you already outcommented that. Are you sure that you was actually running the code as shown in the question? Clean, rebuild, redeploy, etc.

一种替代方法是让您的UserSessionBean实现 HttpSessionBindingListener ,然后在

An alternative is to let your UserSessionBean implement HttpSessionBindingListener and then do the job in the valueUnbound() method.

@ManagedBean
@SessionScoped
public class UserSessionBean implements HttpSessionBindingListener {

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

    // ...
}

这篇关于从HttpSessionListener获取SessionScoped bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆