Bir managedBean den farklı bir managedBean e erişim

Bir managed bean icerisinden farkli bir managed bean e ulasmak istiyorsak faces-config icersinde parametre olarak gosterip inject edebiliriz.

Faces-config.xml icerisinde asagidaki gibi bir tanim yapiyoruz.

     <managed-bean>
      <managed-bean-name>managedBeanBir</managed-bean-name>
      <managed-bean-class>ManagedBeanBir</managed-bean-class>
      <managed-bean-scope>request</managed-bean-scope>
      <managed-property>
       <property-name>m2</property-name>
       <property-class>ManagedBeanIki</property-class>
       <value>#{managedBeanIki}</value>
      </managed-property>
     </managed-bean>
     <managed-bean>
      <managed-bean-name>managedBeanIki</managed-bean-name>
      <managed-bean-class>ManagedBeanIki</managed-bean-class>
      <managed-bean-scope>request</managed-bean-scope>
     </managed-bean>

Bu durumda m2 managedBeanBir de asagidaki gibi kullanilabilir.

    public class ManagedBeanBir {
     private String ad;
     private ManagedBeanIki m2;
     
     public void kaydet() {
      System.out.println("ad = " + ad);
      System.out.println("m2.soyad = " + m2.getSoyad());
     }
     …
    }

Bunun alternatifi olarak FacesContext icerisindeki ELContext ve ELResolver kullanilabilir.

    public class ManagedBeanBir {
     private String ad;
     private ManagedBeanIki m2;
     
     public void kaydet() {
      System.out.println("ad = " + ad);
      System.out.println("m2.soyad = " + m2.getSoyad());
      ManagedBeanIki m2Yeni = (ManagedBeanIki) FacesContext.getCurrentInstance()
        .getELContext().getELResolver().getValue(
          FacesContext.getCurrentInstance().getELContext(), null,
          "managedBeanIki");
      System.out.println("m2Yeni.getSoyad() = " + m2Yeni.getSoyad());
     }
     …
    }
No comments yet.

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>