JSF – 10 – Managed Bean & CDI Bean Scopes – 02

Merhaba Arkadaslar
Bu bolumde Managed Bean & CDI Bean ler icin scope yani yasam alanlari konusuna devam edecegiz.
Burada su yasam alanlarini (scope) inceleyecegiz

  • View Scope
  • None Scope

View Scope

View Scope ( @ViewScoped ) Managed Bean’ler sadece ayni view /page boyunca yasayacaktir, farkli page icin yasam alani (scope ) sonlanir. View Scope xhtml sayfamizda Ajax kullanildiginda kullanisli olmaktadir.

Onceki bolumlerde basit bir Ajax ornegi yapmistik. Bu ornegimizde SessionScoped kullandik , eger bean’in sadece sayfa boyunca yasamasini istersek bu durumda ViewScoped kullanabiliriz.

ViewScopedBean.java

package _13.view.scoped;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class ViewScopedBean implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private String name;
	private String password;
	private int counter;

	//getters and setters

	public int getCounter() {
		return ++counter;
	}

	public void setCounter(int counter) {
		this.counter = counter;
	}

	public String getGreeting() {
		if (name != null && name.length() > 0) {
			return "Welcome to www.injavawetrust.com , " + name + "!";
		} else {
			return "";
		}
	}

	public int getHashCode() {
		return super.hashCode();
	}
}

ViewScopedBean sinifimizi inceledigimizde getCounter metodumuzda ++counter yapiyoruz.
getGreeting metodunu onceki ajax orneklerimizde de kullanmistik. Burada yine getHashCode metodu bize hashCode bilgisini donmektedir.

Not : ViewScoped bir bean mutlaka Serializable olmalidir.

loginViewScoped.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core">
<h:head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>ViewScoped Bean </title>
</h:head>
<h:body>
	<h:form prependId="false">
		<h3>Please enter your name and password.</h3>
		<table>
			<tr>
				<td>Name:</td>
				<td><h:inputText value="#{viewScopedBean.name}" id="nameId" /></td>
			</tr>
			<tr>
				<td>Password:</td>
				<td><h:inputSecret value="#{viewScopedBean.password}" id="passwordId"/></td>
			</tr>
		</table>
		<p>

			<!--<h:commandButton value="Login" action="welcome" /> -->

			<h:commandButton value="Login">
				<f:ajax execute="nameId" render="out out2 out3" />
			</h:commandButton>
		</p>
		
		<p>
			<h:link value="Page1" outcome="page1" />
		</p>

		<h3>
			<h:outputText id="out" value="#{viewScopedBean.greeting}" />
		</h3>
		
		<h3>
			Counter : <h:outputText id="out2" value="#{viewScopedBean.counter}" />
		</h3>
		
		<h3>
			HashCode : <h:outputText id="out3" value="#{viewScopedBean.hashCode}" />
		</h3>

	</h:form>
</h:body>
</html>

xhtml dosyamizi inceledigimizde h:commandButton component’i icerisinde f:ajax componentini kullandik. render attribute’de araya bosluk birakarak h:outputText component’lerini id bilgisini veriyoruz.

Ornegimizi calistirdigimizda ;

viewedscoped bean1

Login butonuna bastigimizda counter artacak , ne kadar basarsak basalim ayni “view” de oldugumuz icin ayni obje kullanilir dolayisiyla hashCode degeri ayni kalacaktir.

viewedscoped bean2

Page1 linkine tikladigimizda bir baska view’e , page1.xhtml sayfasina gecis yaptigimiz icin burada ViewScopedBean objemizin yasam alani son bulacaktir. Dolayisiyla “Levent” erisim saglanamayacaktir ;

viewedscoped bean3

None Scope

NoneScoped bean tek basina kullanildiginda yasam alani son derece kisitli olmaktadir.
Her bir EL (Expression Language) ifadesinde yeni bir bean objesi olusturulmaktadir.

Yani su ifade icin 3 tane NoneScopedBean objesi olusacaktir. Bu bean’lerin yasam alani property ulastiktan sonra son bulmaktadir.

{noneScopedBean.myPropery}
{noneScopedBean.myPropery}
{noneScopedBean.myPropery}

Bunu gormek adina ornerk xhtml sayfamizda 3 kere hashCode bilgisini yazdirabiliriz. Her defasinda farkli bir bean objesi olusacagi icin farkli hashCode degeri yazacaktir.

NoneScopedBean.java

package _14.none.scoped;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.NoneScoped;

@ManagedBean
@NoneScoped
public class NoneScopedBean {
	private String name;
	private String password;
	private int counter;

	public NoneScopedBean(){
		System.out.println("Constructed!");
	}
	
	//getters and setters

	public int getHashCode() {
		return super.hashCode();
	}
	
	public String getGreeting() {
		if (name != null && name.length() > 0) {
			return "Welcome to www.injavawetrust.com , " + name + "!";
		} else {
			return "";
		}
	}
}

Bir onceki ornekte kullandigimiz xhtml ornegini burada da kullanabiliriz extra olarak birden fazla hashCode bilgisini yazdiracagiz.

loginNoneScoped.xhtml

		<p>
			<h:commandButton value="Login">
				<f:ajax execute="nameId" render="out out2 out3 out4 out5" />
			</h:commandButton>
		</p>
		
		<h3>
			<h:outputText id="out" value="#{noneScopedBean.greeting}" />
		</h3>
		
		<h3>
			Counter : <h:outputText id="out2" value="#{noneScopedBean.counter}" />
		</h3>
		
		<h3>
			HashCode : <h:outputText id="out3" value="#{noneScopedBean.hashCode}" />
		</h3>
		
		<h3>
			HashCode : <h:outputText id="out4" value="#{noneScopedBean.hashCode}" />
		</h3>
		
		<h3>
			HashCode : <h:outputText id="out5" value="#{noneScopedBean.hashCode}" />
		</h3>

Ornegimizi calistirdigimizda Console da 6 defa “Constructed” yazildigini gorebiliriz. xhtml dosyamizda 6 defa EL (Expression Language) kullandik. Dolayisiyla 6 tane bean objemiz olustu ve yapilandirimiz 6 defa calisti.

nonescoped bean1

Name alanini doldurup Login dedigimizde bean yasam alani disinda olacagi icin mesaji goremeyiz. Benzer sekilde her defasinda bean olustugu icin Counter degerimiz de hep 1 olarak kalacaktir.

Bir baska senaryo olarak , None Scoped Bean’leri @ManagedProperty ile inject ettigimizde , inject edilen ManagedBean hangi scope’sa None Scoped Bean de o scope’a sahip olacaktir.

SessionScopedInjectBean.java

package _14.none.scoped;

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

@ManagedBean
@SessionScoped
public class SessionScopedInjectBean {

	// @ManagedProperty annotation'i ile DI islemini JSF'e birakiyoruz.
	@ManagedProperty(value = "#{noneScopedMessageBean}")
	private NoneScopedMessageBean noneScopedMessageBean;

	public NoneScopedMessageBean getNoneScopedMessageBean() {
		return noneScopedMessageBean;
	}

	// Setter Injection'in gerceklesmesi icin setter metodu tanimlanmalidir.
	public void setNoneScopedMessageBean(NoneScopedMessageBean noneScopedMessageBean) {
		this.noneScopedMessageBean = noneScopedMessageBean;
	}

	@ManagedProperty("admin")
	// @ManagedProperty annotaion ile String tipi icin de DI islemi yapabiliriz.
	// name property'si icin initialize islemini gerceklestirebiliriz.
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

SessionScopedInjectBean @SessionScoped olarak tanimlandi ve @ManagedProperty annotation ile NoneScopedMessageBean ‘i inject ediyoruz.  NoneScopedMessageBean ise @NoneScoped olarak tanimlidir.

NoneScopedMessageBean.java

package _14.none.scoped;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.NoneScoped;

@ManagedBean
@NoneScoped
public class NoneScopedMessageBean {

	private String message = "Welcome to www.injavawetrust.com , ";

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}

sessionScopedInjectBean.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core">
<h:head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>NoneScoped Bean @ManagedProperty Example </title>
</h:head>
 
    <h:body>
    	<h3>@ManagedProperty Example</h3>
    	<h:form>
    	   <h:inputText value="#{sessionScopedInjectBean.name}"></h:inputText>
		    
		    <p>
		    <h:commandButton value="Login Page Redirect" action="page1?faces-redirect=true" />
			</p>
    	</h:form>
 
    </h:body>
</html>

xhtml sayfamizda Page Redirect islemi sonrasinda page1.xhtml sayfasina gidiyoruz.

page1.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html">
<h:head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>Page1</title>
</h:head>
<h:body>
	<h3>NoneScoped Bean example - page1</h3>
	<h3>Welcome !
		#{sessionScopedInjectBean.noneScopedMessageBean.message}
		#{sessionScopedInjectBean.name}</h3>

</h:body>
</html>

page1.xhtml sayfamizda dikkat edecek olursak @SessionScoped olarak tanimladigimiz SessionScopedInjectBean bean’i uzerinden NoneScopedBean message property alanina ulasiyoruz. NoneScopedBean @NoneScopedBean olarak tanimli fakat inject edildigi icin Session Scope olarak calismaktadir. Dolayisiyla sorunsuz sekilde ulasim saglanacaktir.

nonescoped bean2

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 *