JSF Ders23 - Sayfa Navigasyonu (Page Navigation)


   Sayfa navigasyonu (Page Navigation) açık (explicit) veya örtülü (implicit) şekilde yapılabilir.

Bu konuda bahsedilecek mevzular
  • Açık (explicit) navigation
  • Örtülü (implicit) navigation
  • Koşullu navigation
  • Statik (Static) navigation
  • Dinamik (Dynamic) navigation
Açık (explicit) Navigation ve Örtülü (implicit) Navigation

Örtülü (implicit) navigationu zaten tüm projelerde yaptığımız için onu uzun uzun açıklama gereği duymuyorum. Kısaca Managed Bean sınıfları üzerinde yapılan navigasyon olarak aklınızda tutabilirsiniz. Form üzerinde basılan buton (commandButton,commanLink) bir metot çağırıyor ve bu metodun geriye dönüş değeri gideceğiniz sayfanın ismi olmuş oluyor.Bu kadar.

 Açık (explicit) navigation faces-config.xml dosyasında belirtilir.


<navigation-rule>
     <from-view-id>/index.xhtml</from-view-id>
   
     <navigation-case>
         <from-outcome>deger1</from-outcome>
         <to-view-id>/sonuc1.xhtml</to-view-id>
     </navigation-case>

     <navigation-case>
         <from-outcome>deger2</from-outcome>
         <to-view-id>/sonuc2.xhtml</to-view-id>
     </navigation-case>

     <navigation-case>
         <from-outcome>deger3</from-outcome>
         <to-view-id>/sonuc3.xhtml</to-view-id>
     </navigation-case>
</navigation-rule>


Gelişi güzel bir explicit navigasyon kuralı yukarıda verilmiştir. Yukarıdaki sayfa yazılan kod şu anlama gelmektedir.

index.xhtml sayfasında bir butona (commandButton,commandLink vs.) bastınız ve geriye deger1 döndürdü. O zaman yönlendirileceğiniz sayfa sonuc1.xhtml sayfasıdır. Geriye deger2’yi döndürürse yönlendirileceğiniz sayfa sonuc2.xhtml sayfasıdır.Geriye deger3 döndürürse de yönlendirileceğiniz sayfa sonuc3.xhtml sayfasıdır.

Farklı bir durum

<navigation-rule>
     <from-view-id>/index.xhtml</from-view-id>
   
     <navigation-case>
          <from-outcome>deger1</from-outcome>
          <to-view-id>/sonuc1.xhtml</to-view-id>
     </navigation-case>

     <navigation-case>
          <from-outcome>deger2</from-outcome>
          <to-view-id>/diger.xhtml</to-view-id>
     </navigation-case>

     <navigation-case>
         <from-outcome>deger3</from-outcome>
         <to-view-id>/diger.xhtml</to-view-id>
     </navigation-case>
</navigation-rule>


Görüldüğü üzere geriye deger2 ve deger3 döndürdüğünde yönlendirileceğiniz sayfa diger.xhtml.
Yani deger1 haricindeki dönüş değerlerinde diger.xhtml sayfasına yönlendiriliyorsunuz.

Bunu şu şekilde ifade edebiliriz.


<navigation-rule>
      <from-view-id>/index.xhtml</from-view-id>
      <navigation-case>
             <from-outcome>deger1</from-outcome>
             <to-view-id>/sonuc1.xhtml</to-view-id>
      </navigation-case>

      <navigation-case>
             <!-- diger tüm durumlar için boş bırakıldı. -->
             <to-view-id>/diger.xhtml</to-view-id>
      </navigation-case>
</navigation-rule>


Bir farklı durum daha


<navigation-rule>
      <from-view-id>/index.xhtml</from-view-id>
      <navigation-case>
              <from-outcome>deger1</from-outcome>
              <to-view-id>/sonuc1.xhtml</to-view-id>
      </navigation-case>
</navigation-rule>

<navigation-rule>
       <from-view-id>/index2.xhtml</from-view-id>
       <navigation-case>
             <from-outcome>deger1</from-outcome>
             <to-view-id>/sonuc1.xhtml</to-view-id>
       </navigation-case>
</navigation-rule>

<navigation-rule>
       <from-view-id>/index.3.xhtml</from-view-id>
       <navigation-case>
              <from-outcome>deger1</from-outcome>
              <to-view-id>/sonuc1.xhtml</to-view-id>
        </navigation-case>
</navigation-rule>


Görüldüğü üzere hangi sayfada olursa olsun geriye deger1 değeri döndüğünde sonuc1 sayfasına yönlendiriliyorsunuz. Yani tüm sayfalarda deger1 geri dönüş değeriyle sonuc1 sayfasına yönlendiriliyorsunuz.

Bu durum şu şekilde ifade edilebilir.


<navigation-rule>
     <from-view-id>*</from-view-id>
     <navigation-case>
           <from-outcome>deger1</from-outcome>
           <to-view-id>/sonuc1.xhtml</to-view-id>
      <navigation-case>
</navigation-rule>


Not: Eğer iki navigasyon kuralı birbiriyle eşleşirse (*) içermeyen kural önceliği alır.

Koşullu Navigation

Navigation işlemi bir koşula bağlıdır. Kısa bir örnek aşağıda verilmiştir.



<navigation-rule>
     <from-view-id>*</from-view-id>
     <navigation-case>
           <from-outcome>deger1</from-outcome>
            <if>#{sinifIsmi.degiskenIsmi}</if>
           <to-view-id>/sonuc1.xhtml</to-view-id>
</navigation-rule>


Koşul if etiketinin içine yazılır.Geriye döndürdüğü değere bağlı olarak yönlendirme işlemi yapılır ya da yapılmaz.

Statik (Static) Navigation


<h:commandButton value="sayfa1'e git" action="#{sinifIsmi.metotIsmi}"/>


commandButtonun action kısmında, ManagedBean sınıfı içindeki bir metodun ismini yukaridaki şekilde verip metodun döndürdüğü değerle o sayfaya yönlendirmek yerine aşağıdaki şekilde direkt olarak sayfanın ismi verilebilir. Bu static navigationa örnektir.


<h:commandButton value="sayfa1'e git" action="sayfa1"/>


Dinamik (Dynamic) Navigation

Action controller metodunun geriye döndürdüğü değerle gidilecek sayfayı dolaylı olarak hesaplamak yerine, doğrudan faces-config.xml dosyasında gidilecek sayfa hesaplanabilir.



<navigation-rule>
       <from-view-id>/index.xhtml</from-view-id>
       <navigation-case>
             <to-view-id>#{test.siradakiSayfa}</to-view-id>
       </navigation-case>
</navigation-rule>


Yorumlar

Bu blogdaki popüler yayınlar

Java SE Ders24 - Composition (Kompozisyon)

Spring Ders20 - Aspect Oriented Programming - AspectJ Annotation Style

JSF Ders30 - Page Template (Sayfa Şablonu)