JSF 中的循环管理bean 检测错误

发布于 2024-10-03 18:17:59 字数 1400 浏览 3 评论 0原文

我必须访问页面 A 和 B,我想在 JSF 中从 A 导航到 B,然后从 B 导航到 A。我在 A 的托管 bean 中设置了托管属性 B,反之亦然,但问题是我收到了类似的错误 托管 Bean 循环检测。

  <managed-bean>
      <managed-bean-name>viewBulkMetalIssueBean</managed-bean-name>
      <managed-bean-class>com.cc.jas.web.manufacturing.bulkmetalissue.ViewBulkMetalIssueBean</managed-bean-class>
      <managed-bean-scope>request</managed-bean-scope>
      <managed-property>
       <property-name>viewJobcardBean</property-name>
       <property-class>com.cc.jas.web.manufacturing.jobcard.ViewJobcardBean</property-class>
       <value>#{viewJobcardBean}</value>
       </managed-property>


     </managed-bean>


    <managed-bean>
      <managed-bean-name>viewJobcardBean</managed-bean-name>
      <managed-bean-class>com.cc.jas.web.manufacturing.jobcard.ViewJobcardBean</managed-bean-class>
      <managed-bean-scope>request</managed-bean-scope>
    <managed-property>
       <property-name>viewBulkMetalIssueBean</property-name>
       <property-class>com.cc.jas.web.manufacturing.bulkmetalissue.ViewBulkMetalIssueBean</property-class>
       <value>#{viewBulkMetalIssueBean}</value>
       </managed-property>


      </managed-bean>

这个问题有任何解决方案或替代解决方案吗?

I have to pages A and B, I want to navigate from A to B and back from B to A in JSF. I set managed property B in managed bean of A and visa versa but problem is i got an error like
MANAGED BEAN CYCLIC DETECTION.

  <managed-bean>
      <managed-bean-name>viewBulkMetalIssueBean</managed-bean-name>
      <managed-bean-class>com.cc.jas.web.manufacturing.bulkmetalissue.ViewBulkMetalIssueBean</managed-bean-class>
      <managed-bean-scope>request</managed-bean-scope>
      <managed-property>
       <property-name>viewJobcardBean</property-name>
       <property-class>com.cc.jas.web.manufacturing.jobcard.ViewJobcardBean</property-class>
       <value>#{viewJobcardBean}</value>
       </managed-property>


     </managed-bean>


    <managed-bean>
      <managed-bean-name>viewJobcardBean</managed-bean-name>
      <managed-bean-class>com.cc.jas.web.manufacturing.jobcard.ViewJobcardBean</managed-bean-class>
      <managed-bean-scope>request</managed-bean-scope>
    <managed-property>
       <property-name>viewBulkMetalIssueBean</property-name>
       <property-class>com.cc.jas.web.manufacturing.bulkmetalissue.ViewBulkMetalIssueBean</property-class>
       <value>#{viewBulkMetalIssueBean}</value>
       </managed-property>


      </managed-bean>

Is there any solution or alternate solution for this problem ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等往事风中吹 2024-10-10 18:17:59

这确实是不可能的。如果没有这种检测,只会导致托管属性设置的无限循环。

为了解决这个问题,只需让“父”bean 在注入后将其自身设置在“子”bean 中即可。

public class Parent {
    private Child child;

    public void setChild(Child child) {
        this.child = child;
        this.child.setParent(this);
    }

    // ...
}

<managed-bean>
    <managed-bean-name>parent</managed-bean-name>
    <managed-bean-class>com.example.Parent</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    <managed-property>
        <property-name>child</property-name>
        <property-class>com.example.Child</property-class>
        <value>#{child}</value>
    </managed-property>
</managed-bean>

<managed-bean>
    <managed-bean-name>child</managed-bean-name>
    <managed-bean-class>com.example.Child</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

That's indeed not possible. Without this detection, it would only lead to an infinite loop of managed property setting.

To go around this, just let the "parent" bean set itself in the "child" bean when it has been injected.

public class Parent {
    private Child child;

    public void setChild(Child child) {
        this.child = child;
        this.child.setParent(this);
    }

    // ...
}

With

<managed-bean>
    <managed-bean-name>parent</managed-bean-name>
    <managed-bean-class>com.example.Parent</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    <managed-property>
        <property-name>child</property-name>
        <property-class>com.example.Child</property-class>
        <value>#{child}</value>
    </managed-property>
</managed-bean>

<managed-bean>
    <managed-bean-name>child</managed-bean-name>
    <managed-bean-class>com.example.Child</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>
习ぎ惯性依靠 2024-10-10 18:17:59

您不能在 JSF 托管 Bean 中进行循环注入。 MyFaces wiki 对此进行了很好的解释。

另一种解决方案是使用 EL 解析器(对于 JSF 1.2)从托管 bean 中的 java 代码查找引用:

ELContext elContext = FacesContext.getCurrentInstance().getELContext();
NeededBean neededBean = (NeededBean) FacesContext.getCurrentInstance().getApplication()
    .getELResolver().getValue(elContext, null, "neededBean");

请参阅 MyFaces wiki 条目 以获得进一步的解释,以及其他 JSF 版本的实现。

You can't have cyclic injections in your JSF managed beans. This is explained nicely in the MyFaces wiki.

An alternate solution is to look up the reference from java code within your managed bean using an EL resolver (for JSF 1.2):

ELContext elContext = FacesContext.getCurrentInstance().getELContext();
NeededBean neededBean = (NeededBean) FacesContext.getCurrentInstance().getApplication()
    .getELResolver().getValue(elContext, null, "neededBean");

See the MyFaces wiki entry for further explanation, and implementations for other JSF versions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文