Primefaces Datatable ve Cloneable Obje

Merhabalar,

Bu yazımızda Primefaces kütüphanesinin datatable komponentini basitçe inceleyeceğiz ve bu derste sadece listeleme işlevini göreceğiz.
Bundan sonraki derslerde datatable’da listelenen bir kaydın güncellenmesini ve silinmesini inceleyeceğiz.

Yine Person örneğimiz üzerinden devam ediyoruz ve bir önceki derste oluşturulan person objesini outputtext ile göstermiştik, bu derste ise
bir datatable içine atıp oluşturulan person objelerini listeleyeceğiz.

Javada her nesne referans gösterildiği için, adrese dayalı bir çalışma şekli olduğu için person objesini her güncellediğimizde adresi güncellenmiş olacaktır.

Aşağıdaki kodda PersonView sınıfındaki displayPersonList metodunda da göreceğiniz gibi oluşturulan objeleri listeye attığımızda her yeni kayıt person referansına bağlı olduğundan dolayı bir önceki kayıtlar da en son güncellenen değere dönüşecektir. Bu gibi durumlarda obje klonlanmalıdır, yani kopyası alınabilmelidir. Dolayısıyla PersonBean sınıfı da cloneable bir sınıfa dönüştürülmelidir.
Çalışan kodlar aşağıdaki gibidir.

View Class

/**
 * 
 */
package person.view;

import java.util.ArrayList;
import java.util.List;

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

import person.bean.PersonBean;

/**
 * @author kanbe
 *
 */
@ManagedBean
@SessionScoped
public class PersonView {

	PersonBean person;
	List<PersonBean> personList;

	@PostConstruct
	public void init() {
		personList = new ArrayList<PersonBean>();
		person = new PersonBean();
		person.setName("caglar");
		person.setSurname("kanber");
		person.setAge(27);
		// setPerson(new PersonBean("caglar", "kanber", 27));
	}

	public void displayPersonList() {
		try {
			personList.add((PersonBean) person.clone());
		} catch (CloneNotSupportedException e) {
			System.out.println(e.getMessage());
		}
	}

	public List<PersonBean> getPersonList() {
		return personList;
	}

	public void setPersonList(List<PersonBean> personList) {
		this.personList = personList;
	}

	public PersonBean getPerson() {
		return person;
	}

	public void setPerson(PersonBean person) {
		this.person = person;
	}
}

Bean Class

/**
 * 
 */
package person.bean;

import java.io.Serializable;

/**
 * @author kanbe
 *
 */
public class PersonBean implements Serializable, Cloneable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3071837601559699454L;
	
	private String name;
	private String surname;
	private int age;

	public String getName() {
		return name;
	}

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

	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public PersonBean() {
		super();
		// TODO Auto-generated constructor stub
	}

	public PersonBean(String name, String surname, int age) {
		super();
		this.name = name;
		this.surname = surname;
		this.age = age;
	}
	
	public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

}

person.xhtml

<!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:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui">
<h:head>
	<title>Primefaces Hello World Example</title>
</h:head>
<h:body>

	<h:form id="PersonalInf">

		<h:panelGrid id="personGrid" columns="6">
			<!-- Bilgiler aliniyor. -->
			<p:outputLabel for="name" value="İsim: "></p:outputLabel>
			<p:inputText id="name" value="#{personView.person.name}" />
			<p:outputLabel for="surname" value="Soy İsim: "></p:outputLabel>
			<p:inputText id="surname" value="#{personView.person.surname}" />
			<p:outputLabel for="age" value="Yaş: "></p:outputLabel>
			<p:inputText id="age" value="#{personView.person.age}" />

			<p:commandButton id="submit" value="Listele"
				actionListener="#{personView.displayPersonList()}"
				update=":PersonalInf:personformTbl"></p:commandButton>
		</h:panelGrid>

		<p:dataTable id="personformTbl" value="#{personView.personList}"
			var="person" rows="10" paginator="true"
			paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
			rowsPerPageTemplate="5,10,15">

			<p:column headerText="Ad">
				<h:outputText value="#{person.name}" />
			</p:column>

			<p:column headerText="Soyad">
				<h:outputText value="#{person.surname}" />
			</p:column>

			<p:column headerText="Yaş">
				<h:outputText value="#{person.age}" />
			</p:column>

		</p:dataTable>

	</h:form>


</h:body>
</html>

Kolay gelsin.

Bu yazı Genel, Primefaces kategorisine gönderilmiş ve , , , , , , , ile etiketlenmiş. Kalıcı bağlantıyı yer imlerinize ekleyin.

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir