【问题标题】:How can I access from one ViewScoped bean to another?如何从一个 ViewScoped bean 访问另一个?
【发布时间】:2014-03-09 08:13:18
【问题描述】:

我计划在 Tomcat 中使用一个带有一些 JSF 托管 Bean 的 *.xhtml 页面。我想成为ViewScoped的所有豆子。我需要将一些数据(通常是一个简单的 POJO)从一个 bean 传递到另一个,但是...

@managedProperty 不起作用。

findBean("someBean") - 未知标识符。

FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("sessionBean"); - 与 SessionScoped bean 一起使用。 ViewScoped 呢?

出于安全原因,我不想通过GET 参数传递整数标识符。 理论上,我可以使用SessionScoped bean 来传输数据,但这是最好的解决方案吗?

是否有任何方法可以从一个 ViewScoped bean 访问另一个,例如 FacesContext.getCurrentInstance()...

我猜,所有ViewScoped bean 都同时实例化,并且生命周期相同。我说的对吗?

感谢您为我浪费时间。

【问题讨论】:

  • @ManagedProperty 应该可以工作。你想在你的 bean 中访问什么?

标签: jakarta-ee tomcat jsf-2


【解决方案1】:

给你:

@ManagedBean
@ViewScoped
public class MyBean implements Serializable {
    @ManagedProperty("#{otherBean}")
    private OtherBean otherBean;
}

@ManagedBean
@ViewScoped
public class OtherBean implements Serializable {
}

从 viewMap 获取 bean 可能有效,但它非常丑陋,您不能/不应该对类字段执行此操作。

【讨论】:

  • 这样的例子不起作用。我为OtherBean 写了getter 和setter,但没有结果。 OtherBean 始终为空。
【解决方案2】:

我找到了!

FacesContext.getCurrentInstance().getViewRoot().getViewMap().get("beanName") 工作正常。

【讨论】:

    猜你喜欢
    • 2016-12-26
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 2013-02-27
    • 2012-11-08
    • 2020-08-15
    • 2011-09-15
    • 2012-09-03
    相关资源
    最近更新 更多