我正在尝试将属性设置为ModelBean
过滤器中的 bean 并在 JSF 控制器中访问此属性IndexController
。
ModelBean 带有注释@SessionScoped
,并在过滤器和控制器中使用@Inject
. 问题是创建了两个单独的实例,我无法访问我在过滤器中设置的属性。
在整个会话期间保持 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();
}
}