我试图将参数从一个页面传递到另一个页面,但是当我按下"详细信息"按钮时,它给了我这个错误:"无法创建托管bean agencyDetailBean。-由表达式#{代理},请求引用的对象的范围小于会话"
的引用管理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;
}
}
我的第二个bean类:
@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;
}
}
最初,. xml页面是这样的,它工作了。
<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>
有什么建议是什么问题和如何解决它?提前感谢!
使用@ManagedBean
,不能将作用域较小的bean注入作用域较大的bean。根据您的情况,将@SessionScoped
注入@RequestScoped
。然后,您可以从@RequestScoped
bean更新@SessionScoped
的任何属性。
关于您的更新,在第二个bean中,您无法从这样的getter获取tAgency
。下面的函数可以完成这项工作。
@PostConstruct
public void init() {
tAgency = agencyBean.gettAgency();
}