繁体   English   中英

如何通过过滤器和EJB访问贯穿会话来保持bean的活动状态

[英]How to keep a bean alive for the through session accessible from filter and EJB

我试图在Filter ModelBean属性设置为bean ModelBean ,并在JSF控制器IndexController访问此属性。

ModelBean带注释@SessionScoped ,它在过滤器和控制器中使用@Inject 问题是创建了两个单独的实例,我无法访问我在filter中设置的属性。

在整个会话期间保持bean活着的最佳方法是什么? 或者可能有更好的方法从过滤器传递数据?

@SessionScoped
public class ModelBean{
    private String deviceId;

    public ModelBean() {
        super();
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }
}
@Provider
public class AuthRequestFilter implements Filter {

    @Inject
    ModelBean model;


    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws
            IOException, ServletException {

        // the device id is set just fine
        model.setDeviceId(deviceId);
        filterChain.doFilter(servletRequest, servletResponse);
        return;
    }
}
@Named(value = "indexController")
public class IndexController {

    @Inject
    ModelBean model;

    // the method **is* called from the xhtml
    public String justAnExample() {
        // this is the problem, the deviceId is null=>
        return model.getDeviceId();
    }
}

感谢@Kukeltje关于查看包裹的建议。 我仍然不知道为什么我使用的包javax.faces.bean.SessionScoped没有保持bean活着,但用javax.enterprise.context.SessionScoped替换它就可以了。 bean现在处于活动状态,数据可以传递。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM