[英]How to keep a bean alive for the through session accessible from filter and EJB
I am trying to set a property to a bean ModelBean
in a Filter and access this property in a JSF controller IndexController
. 我试图在Filter
ModelBean
属性设置为bean ModelBean
,并在JSF控制器IndexController
访问此属性。
The ModelBean is annotated @SessionScoped
and it is used in the filter and controller with @Inject
. ModelBean带注释
@SessionScoped
,它在过滤器和控制器中使用@Inject
。 The problem is that two separate instances are created and I cannot access the property that I set in filter. 问题是创建了两个单独的实例,我无法访问我在filter中设置的属性。
What would be the best way to keep the bean alive throughout the session? 在整个会话期间保持bean活着的最佳方法是什么? Or maybe there is a better method to pass data from the filter?
或者可能有更好的方法从过滤器传递数据?
@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();
}
}
Thanks to @Kukeltje for the suggestion to look into the packages. 感谢@Kukeltje关于查看包裹的建议。 I still Don't know why the package
javax.faces.bean.SessionScoped
that I was using did not keep the bean alive, but replacing it with javax.enterprise.context.SessionScoped
did the trick. 我仍然不知道为什么我使用的包
javax.faces.bean.SessionScoped
没有保持bean活着,但用javax.enterprise.context.SessionScoped
替换它就可以了。 The bean is now alive and the data can be passed. bean现在处于活动状态,数据可以传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.