JSF – 11 – Managed Bean & CDI Bean Scopes – 03

Merhaba arkadaslar,
Bu bolumde CDI bean’lerine ozel olan Conversation Scope konusunu inceleyecegiz.

Conversation Scope

Conversation Scope sadece CDI bean’leriyle kullanilabilir , bunun icin @ConversationScoped annotation kullanabiliriz. CDI bean ile calisacagimiz icin ornegimizi Glassfish gibi bir Application Server uzerinde deploy etmemiz gereklidir. Hatirlayacagimiz gibi Apache Tomcat gibi sadece Servlet Container ozelligi gosteren uygulamalar CDI bean icin yeterli olmayacaktir.
Glassfish kurulumunu ve Application Server ozelligi gosteren Apache TomEE kurulum ve gerekli ayarlamalari onceki bolumlerde inceledik.

Ornegin yasam alani bir kac view olan bir bean olusturmak istiyoruz. Bu istegimizi karsilayacak sckle Conversation Scope olacaktir.

Bir Session’da bir den fazla Conversation Scope olabilir.

Conversation Scope’un baslangic ve bitis noktalarini kendimiz belirleriz. Bunun icin javax.enterprise.context.Conversation arabiriminden yararlaniriz.

ConversationScopedBean.java

import java.io.Serializable;

import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;

@Named
@ConversationScoped
public class ConversationScopedBean implements Serializable {
	@Inject
	private Conversation conversation;

	public Conversation getConversation() {
		return conversation;
	}
}

@ConversationScoped annotation CDI bean’leri icin gecerli oldugu icin @Named annotation ile birlikte kullaniyoruz.
Daha sonrasinda javax.enterprise.context.Conversation tipinde bir instance variable tanimliyoruz. Burada Dependency Injection icin @Inject annotation’ini kullaniyoruz.

Simdi de Conversation’i baslatip bitirmek icin metot ekleyelim.

package _15.conversation.scoped;

import java.io.Serializable;

import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;

@Named
@ConversationScoped
public class ConversationScopedBean implements Serializable {

	.....
	public void beginConversation() {
                //Conversation basladiginda transient state'tir
		if (conversation.isTransient()) {
			conversation.begin();
		}

	}

	public void endConversation() {

		if (!conversation.isTransient()) {
			conversation.end();
		}

	}

	...

}

Bu ornegimizde Employee , Address ve ContactInfo olmak uzere 3 tane POJO muz olacak.

Employee.java

package _15.conversation.scoped;

public class Employee {

	private String name;
	private String password;

	private Address address;
	private ContactInfo contactInfo;

	public Employee() {
		address = new Address();
		contactInfo = new ContactInfo();
	}
        //getters and setters
        //..........
}

Address.java

package _15.conversation.scoped;
public class Address {

	private String city;
	private String district;
	private String street;
        //getters and setters
        //..........
}

ContactInfo.java

package _15.conversation.scoped;

public class ContactInfo {

	private String homePhone;
	private String mobilePhone;
	private String email;
        //getters and setters
        //..........
}

Yapilandiricimizda/constructor Employee objemizi olusturalim. Bununla birlikte start ve finish metounda beginConversation ve endConversation metotlarini cagiracagiz bu metotlari basit navigation icin kullanacagiz.

ConversationScopedBean.java

package _15.conversation.scoped;

import java.io.Serializable;

import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;

@Named
@ConversationScoped
public class ConversationScopedBean implements Serializable {

	private static final long serialVersionUID = 1L;

	private Employee employee;
	public ConversationScopedBean() {
		employee = new Employee();
	}

	@Inject
	private Conversation conversation;

	public Conversation getConversation() {
		return conversation;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public void beginConversation() {
		//Conversation basladiginda transient state'tir
		if (conversation.isTransient()) {
			conversation.begin();
		}

	}

	public void endConversation() {

		if (!conversation.isTransient()) {
			conversation.end();
		}

	}

	public String start() {
		beginConversation();
		return "02.loginConversationScoped.xhtml?faces-redirect=true";
	}

	public String finish() {
		endConversation();
		return "06.afterEndConversation.xhtml?faces-redirect=true";
	}

}

Xhtml sayfalarimizi inceleyelim.
01.startConversation.xhtml

...
	<h:form>
		<h:commandLink value="Stat Conversation!" action="#{conversationScopedBean.start}"/>
	</h:form>
...

01.startConversation.xhtml ‘de h:commandLink component’ini kullaniyoruz. Bu noktada Conversation’ini baslatmak icin start metodunu cagiriyoruz.

02.loginConversationScoped.xhtml

....
	<h:form>
		<h3>Please enter your name and password.</h3>
		<table>
			<tr>
				<td>Name:</td>
				<td><h:inputText value="#{conversationScopedBean.employee.name}" /></td>
			</tr>
			<tr>
				<td>Password:</td>
				<td><h:inputSecret value="#{conversationScopedBean.employee.password}" /></td>
			</tr>
		</table>

		<p>
			<h:commandButton value="Next To Address" action="03.address.xhtml" />
		</p>
		
	</h:form>
....

02.loginConversationScoped.xhtml dosyamiz onceki orneklerde kullandigimiz gibi , h:CommandButton butonunda bir sonraki view’e gidiyoruz.

03.address.xhtml

....
	<h:form>
		<h3>Please enter your Address info.</h3>
		<table>
			<tr>
				<td>City:</td>
				<td><h:inputText value="#{conversationScopedBean.employee.address.city}" /></td>
			</tr>
			<tr>
				<td>District:</td>
				<td><h:inputText value="#{conversationScopedBean.employee.address.district}" /></td>
			</tr>
			
			<tr>
				<td>Street:</td>
				<td><h:inputText value="#{conversationScopedBean.employee.address.street}" /></td>
			</tr>
		</table>

		<p>
			<h:commandButton value="Back" action="02.loginConversationScoped.xhtml" />
		</p>
		
		<p>
			<h:commandButton value="Next To Contact Info" action="04.contactInfo.xhtml" />
		</p>
		
	</h:form>
....

04.contactInfo.xhtml

....
	<h:form>
		<h3>Please enter your Contact info.</h3>
		<table>
			<tr>
				<td>Home Phone:</td>
				<td><h:inputText value="#{conversationScopedBean.employee.contactInfo.homePhone}" /></td>
			</tr>
			<tr>
				<td>Mobile Phone:</td>
				<td><h:inputText value="#{conversationScopedBean.employee.contactInfo.mobilePhone}" /></td>
			</tr>
			
		   <tr>
				<td>Email :</td>
				<td><h:inputText value="#{conversationScopedBean.employee.contactInfo.email}" /></td>
			</tr>
		</table>

		<p>
			<h:commandButton value="Back" action="03.address.xhtml" />
		</p>
		
		<p>
			<h:commandButton value="Finish" action="05.endConversation.xhtml" />
		</p>
		
	</h:form>
....

05.endConversation.xhtml

...
	<p>05.endConversation.xhtml</p>
	<p>#{conversationScopedBean.employee.name}</p>
	<p>#{conversationScopedBean.employee.password}</p>
	<p>#{conversationScopedBean.employee.address.city}</p>
	<p>#{conversationScopedBean.employee.address.district}</p>
	<p>#{conversationScopedBean.employee.address.street}</p>
	<p>#{conversationScopedBean.employee.contactInfo.homePhone}</p>
	<p>#{conversationScopedBean.employee.contactInfo.mobilePhone}</p>
	<p>#{conversationScopedBean.employee.contactInfo.email}</p>

	<h:form>
		<h:commandLink value="End Conversation!" action="#{conversationScopedBean.finish}" />
	</h:form>
....

endConversation.xhtml sayfamizda finish metodunu cagirarak conversation’ini sonlandiririrz ve 06.afterEndConversation.xhtml sayfasina Page Redirect islemi yapilir. 06.afterEndConversation.xhtml sayfasinda Conversation gecerli olmadigi icin bean’e ulasim SAGLANAMAZ!

06.afterEndConversation.xhtml

....
	<!-- conversation tamamlandigi icin ulasim saglanamaz! -->
	<h1>06.afterEndConversation.xhtml</h1>
	
	<p>#{conversationScopedBean.employee.name}</p>
	<p>#{conversationScopedBean.employee.password}</p>
	<p>#{conversationScopedBean.employee.address.city}</p>
	<p>#{conversationScopedBean.employee.address.district}</p>
	<p>#{conversationScopedBean.employee.address.street}</p>
	<p>#{conversationScopedBean.employee.contactInfo.homePhone}</p>
	<p>#{conversationScopedBean.employee.contactInfo.mobilePhone}</p>
	<p>#{conversationScopedBean.employee.contactInfo.email}</p>
....

Ornegimizi 01.startConversation.xhtml ile calistiralim ve linke tiklayip devam edelim.

conversationscoped bean start1

Gerekli alanlari doldurup next ile devam edelim ve son olarak 05.endConversation.xhtml sayfasina geldigimizde onceki sayfalarda doldugumuz alanlari gorebiliriz.

conversationscoped bean

End Conversation linkine tikladigimizda 06.afterEndConversation.xhtml sayfamiz acilir. Burada Conversation tamamlandigi icin bean’e ulasim saglanamaz.

conversationscoped bean afterEndConversation

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 *