JSF – 15 – Dynamic Navigation

Merhaba Arkadaslar,
Onceki bolumde Static Navigation konusunu incelemistik. Bu bolumde Dynamic Navigation konusuna giris yapacagiz.
Buyuk projeleri dusundugumuzde Static Navigation yetersiz kalacaktir. Bunun icin daha esnek bir yaklasim olarak Dynamic Navigation kullanilacaktir.

Dynamic Navigation i gerceklestirmek icin faces-config.xml dosyasindan yararlanacagiz. Bu dosyaya <navigation-rule> eklememiz gerekecek.

<navigation-rule>
	<from-view-id>/login.xhtml</from-view-id>
	<navigation-case>
		<from-outcome>success</from-outcome>
		<to-view-id>/welcome.xhtml</to-view-id>
	</navigation-case>
</navigation-rule>

login.xhtml sayfasindan ( from-view-id ) outcome bilgisi ( from-outcome ) success olarak geldiginde welcome.xhtml sayfasina navigation( to-view-id) islemi yap anlamina gelmektedir.

Birden fazla <navigation-case> elemanini ayni  <navigation-rule> elaman icerisinde kullanabiliriz (merge).

<navigation-rule>
	<from-view-id>/login.xhtml</from-view-id>
	<navigation-case>
		<from-outcome>success</from-outcome>
		<to-view-id>/welcome.xhtml</to-view-id>
	</navigation-case>

	<navigation-case>
		<from-outcome>failure</from-outcome>
		<to-view-id>/newuser.xhtml</to-view-id>
	</navigation-case>
</navigation-rule>

from-vied-id elemanini kullanmazsak tum outcome bilgisi icin tek bir sayfaya yonlendirme/navigation yapabiliriz. Ornegin tum logout outcome degeri icin kullanmak isteyecegimiz tek bir xhtml sayfasi olacaktir.

<navigation-rule>
	<navigation-case>
	<from-outcome>logout</from-outcome>
	<to-view-id>/loggedOut.xhtml</to-view-id>
	</navigation-case>
</navigation-rule>

Ornek Uygulama

Ornegimizde basit bir login-locked mekanizmasi yapacagiz.

DynamicNavigationBean.java

package _19.dynamic.navigation;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class DynamicNavigationBean {
	private String name;
	private String password;
        //getters and setters

	private int counter = 0;
	private boolean locked = false;

	public String login() {
		// Do some processing
		if (name.equals("Levent") && password.equals("java")) {
			counter = 0;
			locked = false;
			return "success";
		}
		counter++;
		if (locked) {
			return "locked";
		}
		return "failure";
	}

	public String checkCounter() {

		if (counter > 3) {
			locked = true;
			return "locked";
		} else
			return "retry";
	}
}

login metodunda name ve password kontrolu yapilmaktadir , test amacli “Levent” ve “java” olarak belirledim.
bu bilgiler girilmediginde geriye “failure” ,locked true oldugu durumda geriye “locked” .

checkCounter metodunda counter 3 ten fazla oldugu durumda locked true yapilmaktadir, aksi durumda “retry” donmektedir.

login.xhtml

...
	<h:form>
		<h3>Please enter your name and password.</h3>
		<table>
			<tr>
				<td>Name:</td>
				<td><h:inputText value="#{dynamicNavigationBean.name}" /></td>
			</tr>
			<tr>
				<td>Password:</td>
				<td><h:inputSecret value="#{dynamicNavigationBean.password}" /></td>
			</tr>
		</table>
		<p>
			<h:commandButton value="Login" action="#{dynamicNavigationBean.login}"></h:commandButton>
		</p>
	</h:form>
...

relogin.xhtml

...
	<h:form>
		<table>
			<tr>
				<td><h:outputText value="Your Username or Password is wrong, please relogin"/></td>
			</tr>
		</table>
		<p>
			<h:commandButton value="Relogin" action="#{dynamicNavigationBean.checkCounter}"></h:commandButton>
		</p>
	</h:form>
...

welcome.xhtml

...
<h:body>
	<h3>Welcome , #{dynamicNavigationBean.name}!</h3>
</h:body>
...

locked.xhtml

...
<h3>Your account is locked!.</h3>
...

faces-config.xml

...
	<navigation-rule>
		<from-view-id>/19.dynamic.navigation/login.xhtml</from-view-id>
		<navigation-case>
			<from-outcome>success</from-outcome>
			<to-view-id>/19.dynamic.navigation/welcome.xhtml</to-view-id>
		</navigation-case>
		<navigation-case>
			<from-outcome>failure</from-outcome>
			<to-view-id>/19.dynamic.navigation/relogin.xhtml</to-view-id>
		</navigation-case>
		<navigation-case>
			<from-outcome>locked</from-outcome>
			<to-view-id>/19.dynamic.navigation/locked.xhtml</to-view-id>
		</navigation-case>
	</navigation-rule>


	<navigation-rule>
		<from-view-id>/19.dynamic.navigation/relogin.xhtml</from-view-id>
		<navigation-case>
			<from-outcome>locked</from-outcome>
			<to-view-id>/19.dynamic.navigation/locked.xhtml</to-view-id>
		</navigation-case>

		<navigation-case>
			<from-outcome>retry</from-outcome>
			<to-view-id>/19.dynamic.navigation/login.xhtml</to-view-id>
		</navigation-case>

	</navigation-rule>
...

/19.dynamic.navigation/login.xhtml sayfasindan outcome olarak success geldiginde /19.dynamic.navigation/welcome.xhtml
failure geldiginde /19.dynamic.navigation/relogin.xhtml locked geldiginde ise /19.dynamic.navigation/locked.xhtml sayfasina navigation olmaktadir.

Ayni mantik diger navigation-rule icin de gecerlidir.

Ornegimizi calistirdigimizda…

invalid login

Gecersiz bir giris yaptigimizda ;

relogin

3 ten fazla basarisiz giris yaptigimizda

locked

Session’i sonlandirip tekrar deneyelim ve basarili bir giris icin test edebiliriz.

Conditional Navigation

Conditional Navigation , JSF 2.0 ile gelen bir ozelliktir. <navigation-case> icerisinde <if> elamanini kullanarak Conditional Navigation islemini gerceklesirebiliriz. Basit bir ornek uzerinde inceleyelim.

ConditionalNavigationBean.java

package _19.dynamic.navigation;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class ConditionalNavigationBean {

	private String property1;
	private int property2;
        //getters and setters

}

conditional.xhtml

...
	<h:form>
		<h3>Please enter propert1 and property2</h3>
		<table>
			<tr>
				<td>Property1:</td>
				<td><h:inputText value="#{conditionalNavigationBean.property1}" /></td>
			</tr>
			<tr>
				<td>Property2:</td>
				<td><h:inputText value="#{conditionalNavigationBean.property2}" /></td>
			</tr>
		</table>
		<p>
			<h:commandButton value="Navigate" action="testOutcome"></h:commandButton>
		</p>
	</h:form>
...

page1.xhtml

...
<h:body>
		<h3>Page1</h3>
</h:body>
...

page2.xhtml

...
<h:body>
		<h3>Page2</h3>
</h:body>
...

page3.xhtml

...
<h:body>
		<h3>Page1</h3>
</h:body>
...

faces-config.xml

...
	<navigation-rule>
		<from-view-id>/19.dynamic.navigation/conditional/conditional.xhtml</from-view-id>
		<navigation-case>
			<from-outcome>testOutcome</from-outcome>
			<if>#{conditionalNavigationBean.property1 =='page1'}</if>
			<to-view-id>/19.dynamic.navigation/conditional/page1.xhtml</to-view-id>
		</navigation-case>
		<navigation-case>
			<from-outcome>testOutcome</from-outcome>
			<if>#{conditionalNavigationBean.property1 =='page2'}</if>
			<to-view-id>/19.dynamic.navigation/conditional/page2.xhtml</to-view-id>
		</navigation-case>
		<navigation-case>
			<from-outcome>testOutcome</from-outcome>
			<if>#{conditionalNavigationBean.property1 =='page3' and conditionalNavigationBean.property2 ==10}</if>
			<to-view-id>/19.dynamic.navigation/conditional/page3.xhtml</to-view-id>
		</navigation-case>
	</navigation-rule>
...

/19.dynamic.navigation/conditional/conditional.xhtml sayfasindan outcome olarak testOutcome geldigi durumda property1 ‘page1’ iken /19.dynamic.navigation/conditional/page1.xhtml , ‘page2’ oldugunda /19.dynamic.navigation/conditional/page2.xhtml property1 ‘page3’ ve property2 10 oldugunda ise /19.dynamic.navigation/conditional/page3.xhtml sayfasina navigation yapilir. Bu sartlarin disinda navigation yapilmaz ayni sayfada kalacaktir.

Ornegimizi calistiralim ; Propert1 icin page1 yazdigimizda page1.xhtml e , page2 icin page2.xhtml , page3 ve 10 yazdigimizda page3.xhtml sayfasina navigation gerceklesecektir.

conditional

from-action

navigation-case elemani icerisinde from-outcome elamanini kullandik. Benzer sekilde from-action elamani da vardir.

2 tane metot tanimlayalim ve ayni outcome degerini donsunler. Bu durumda from-action elemanini kullanmamiz gerekir.

FromActionNavigationBean.java

package _19.dynamic.navigation;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class FromActionNavigationBean {

	public String navigate1() {
		return "navigate";
	}

	public String navigate2() {
		return "navigate";
	}
}

fromAction.xhtml

...
	<h:form>
		<p>
			<h:commandButton value="Page1" action="#{fromActionNavigationBean.navigate1}"></h:commandButton>
		</p>
		
		<p>
			<h:commandButton value="Page2" action="#{fromActionNavigationBean.navigate2}"></h:commandButton>
		</p>
	</h:form>
...

page1.xhtml

...
<h:body>
		<h3>Page1</h3>
</h:body>
...

page2.xhtml

...
<h:body>
		<h3>Page2</h3>
</h:body>
...

faces-config.xml

...
	<navigation-rule>
		<from-view-id>/19.dynamic.navigation/from.action/fromAction.xhtml</from-view-id>
		<navigation-case>
			<from-action>#{fromActionNavigationBean.navigate1}</from-action>
			<from-outcome>navigate</from-outcome>
			<to-view-id>/19.dynamic.navigation/from.action/page1.xhtml</to-view-id>
		</navigation-case>
		<navigation-case>
			<from-action>#{fromActionNavigationBean.navigate2}</from-action>
			<from-outcome>navigate</from-outcome>
			<to-view-id>/19.dynamic.navigation/from.action/page2.xhtml</to-view-id>
		</navigation-case>
	</navigation-rule>
...

2 <navigation-case> durumu icin de outcome degeri navigate1 dir. Burada farki ortaya koyacak sey <from-action> elemanidir.

Ornegimizi calistirdigimizda ;

from action

Page1 butonuna tikladigimizda page1.xhtml , Page2 butonuna tikladigimizda page2.xhtml sayfasina navigation islemi gerceklesecektir.

Redirection

Page Redirection icin outcome degeri sonuna faces-redirect=true parametre bilgisini ekliyorduk.
Bunun icin <navigation-case/> elemani icerisinde <redirect/> elamanini kullanmamiz yeterli olacaktir. Basit bir uygulama yapalim.

redirection.xhtml

...
 	<h:form>
		<p>
			<h:commandButton value="Navigate" action="redirectOutcome"></h:commandButton>
		</p>
	</h:form>
...

page1.xhtml

...
<h:body>
		<h3>Page1</h3>
</h:body>
...

faces-config.xml

...
	<navigation-rule>
		<from-view-id>/19.dynamic.navigation/redirection/redirection.xhtml</from-view-id>
		<navigation-case>
			<from-outcome>redirectOutcome</from-outcome>
			<to-view-id>/19.dynamic.navigation/redirection/page1.xhtml</to-view-id>
			<redirect/>
		</navigation-case>
	</navigation-rule>
...

Kaynak kodlar : Injavawetrust.jsf

Yazimi burada sonlandiriyorum.
Herkese Bol Javali Gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6 Web Component Developer

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *