chetan
chetan

Reputation: 3255

cyclic managebean detection error in JSF

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 ?

Upvotes: 4

Views: 5660

Answers (2)

BalusC
BalusC

Reputation: 1108852

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>

Upvotes: 14

Brian Leathem
Brian Leathem

Reputation: 4639

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.

Upvotes: 5

Related Questions