将参数从一个页面传递到另一个页面 - 表达式引用的对象的范围比引用的托管bean错误短

时间:2014-05-03 20:07:56

标签: java jsf primefaces

我正在尝试将参数从一个页面传递到另一个页面,但是当我按下“详细信息”按钮时,它会给我这个错误:“无法创建托管bean agencyDetailBean。发现了以下问题: - 范围表达式#{agency},request引用的对象比引用的托管bean(agencyDetailBean)会话范围短“

这是我的.xhtml页面的一部分:

<p:dataGrid var="agency" value="#{agencyBean.agencyList}" columns="1"
    rows="5" paginator="true" paginatorPosition="bottom">
    <p:growl id="growl" showDetail="true" />
    <p:fieldset legend="${agency.tUser.name}" toggleable="true">
        <p:ajax event="toggle" listener="#{fieldsetBean.handleToggle}"
            update="growl" />
        <h:panelGrid columns="2" cellpadding="3" cellspacing="1">
            <h:panelGrid columns="2" cellpadding="3" cellspacing="1">
                Name: <h:outputText value="${agency.tUser.name}" />
                Phone: <h:outputText value="${agency.tUser.phone}" />
            </h:panelGrid>

            <h:commandButton value="Details" action="agencyDetail">
                <f:setPropertyActionListener target="#{agencyBean.tAgency}"
                    value="${agency}" />
            </h:commandButton>
        </h:panelGrid>

    </p:fieldset>
</p:dataGrid>

我的第一个豆子:

@ManagedBean(name = "agencyBean")
@SessionScoped
public class AgencyBean implements Serializable {

    private TAgency tAgency = new TAgency();
    private List<TAgency> agencyList;

    public List<TAgency> getAgencyList() {
        return agencyList;
    }

    public void setAgencyList(List<TAgency> agencyList) {
        this.agencyList = agencyList;
    }

    @PostConstruct
    public void init() {
        EntityManager em = HibernateUtil.getEntityManager();
        Query q = em.createQuery("select u from TAgency u");
        agencyList = q.getResultList();
    }

    public TAgency gettAgency() {
        return tAgency;
    }

    public void settAgency(TAgency tAgency) {
        this.tAgency = tAgency;
    }
}

我的第二个豆类:

@ManagedBean(name = "agencyDetailBean", eager = true)
@SessionScoped
public class AgencyDetailBean implements Serializable {
    private static final long serialVersionUID = 1L;

    @ManagedProperty(value = "#{tAgency}")
    private AgencyBean agencyBean;

    private TAgency tAgency;

    public TAgency gettAgency() {
        if (agencyBean != null) {
            tAgency = agencyBean.gettAgency();
        }
        return tAgency;
    }

    public void setAgencyBean(AgencyBean agency) {
        this.agencyBean = agency;
    }
}

最初,.xhml页面就像这样,它起作用了。

<p:growl id="growl" showDetail="true" />
<c:forEach items="#{agencyBean.agencyList}" var="agency">
    <p:fieldset legend="${agency.tUser.name}" toggleable="true">
        ...
            <h:commandButton value="Details" action="agencyDetail">
                <f:setPropertyActionListener target="#{agencyDetailBean.agency}"<br/>
                    value="${agency}" />
            </h:commandButton>
        </h:panelGrid>
    </p:fieldset>
</c:forEach>

任何建议是什么问题以及如何解决? 提前谢谢!

1 个答案:

答案 0 :(得分:1)

使用@ManagedBean,您无法将范围较小的bean注入范围更大的bean中。对于您的情况,请将@SessionScoped注入@RequestScoped。然后,您可以从@SessionScoped bean更新@RequestScoped的任何属性。

关于你的更新,在第二个bean中,你不能从这样的getter获得tAgency。以下功能可以完成这项工作。

@PostConstruct
public void init() {
    tAgency = agencyBean.gettAgency();
}