无法从SessionScoped bean获取viewScoped bean

时间:2014-10-30 15:41:03

标签: java jsf managed-bean view-scope session-scope

我需要从sessionScoped托管bean获取viewScoped托管bean。我首先尝试使用 @ManagedProperty 注释,但我立即发现它不起作用,我知道为什么。其次,我尝试使用以下方法:

Map<String, Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();   
PulsantieraBean pulsantiera = (PulsantieraBean) viewMap.get("#{pulsantiera}");

但是当我在调试中访问对象时,pulsantiera为null,而viewMap对象只包含另一个viewScoped托管bean。 PulsantieraBean 类以下列方式声明:

@ViewScoped
@ManagedBean(name = NomeBean.PULSANTIERA)
public class PulsantieraBean extends ControllerBaseBean implements Serializable {

而我执行请求的类被声明为以下

@SessionScoped
@ManagedBean(name = NomeBean.IDF_TAB_IMPORTI)
public class IdfTabImportiBean extends ControllerBaseBean implements Serializable {

我在一个方法中执行请求,我确信viewScoped托管bean PulsantieraBean 存在。

任何帮助将不胜感激。 谢谢, 马可

1 个答案:

答案 0 :(得分:-1)

@Named(value = "controladorMB")
@SessionScoped
public class ControladorMB implements Serializable {}

从ListadoFacturasMB调用ControladorMB

@Named(value = "listadoFacturasMB")
@SessionScoped
public class ListadoFacturasMB implements Serializable {

public void example() {
    try {
        FacesContext ctx = FacesContext.getCurrentInstance();
        ValueExpression vex =ctx.getApplication().getExpressionFactory().createValueExpression(ctx.getELContext(), "#{controladorMB}", ControladorMB.class);
        ControladorMB gMB = (ControladorMB) vex.getValue(ctx.getELContext());
       } catch (Exception e) {
        JsfUtil.addErrorMessage(e, "Error: buscarPersonalizado() " + e.getMessage());
       }
   }
}