Java server faces’ta dataTable’dan satır silme
Merhaba arkadaşlar bu yazımda jsf’te dataTable’den Satır silmeyi yapacağız. Aslında oldukça basit bu işlem , ilk önce nesnemizi oluşturalım
Urun.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
package com.turkishh; import java.math.BigDecimal; /** * * @author mkilic */ public class Urun { String siparisNo; String urunIsmi; BigDecimal fiyat; public Urun(String siparisNo, String urunIsmi, BigDecimal fiyat) { this.siparisNo = siparisNo; this.urunIsmi = urunIsmi; this.fiyat = fiyat; } public String getSiparisNo() { return siparisNo; } public void setSiparisNo(String siparisNo) { this.siparisNo = siparisNo; } public String getUrunIsmi() { return urunIsmi; } public void setUrunIsmi(String urunIsmi) { this.urunIsmi = urunIsmi; } public BigDecimal getFiyat() { return fiyat; } public void setFiyat(BigDecimal fiyat) { this.fiyat = fiyat; } } |
Sonrasında bean’imizi oluşturalım ve içeri static liste oluşturduk.İçeride listemizden sayfadan gönderdiğimiz objeyi silen bir methodumuz var ” silUrun(Urun urun) ;” isminde.
1 |
<strong>UrunBean.java</strong> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
package com.turkishh; import java.io.Serializable; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; /** * * @author mkilic */ @ManagedBean @SessionScoped public class UrunBean implements Serializable { private static final long serialVersionUID = 1L; private static final ArrayList<Urun> urunList = new ArrayList<Urun>(Arrays.asList( new Urun("T0001", "Arduino Uno", new BigDecimal("700.00")), new Urun("T0002", "LilyPad Arduino", new BigDecimal("500.00")), new Urun("T0003", "UDOO QUAD", new BigDecimal("11600.00")), new Urun("T0004", "Raspberry Pi", new BigDecimal("5200.00")), new Urun("T0005", "Intel® Galileo", new BigDecimal("100.00")) )); public ArrayList<Urun> getUrunList() { return urunList; } public String silUrun(Urun urun) { urunList.remove(urun); return null; } } |
Şimdi ise sayfamızı yazıyoruz burada dikkat edilmesi gereken, tablomuzdaki her satır için eklediğimiz commandLink ‘e action oalrak silUrun methodumuzu eklemek ve parametre olarak ise objeyi vermek . Bu durumda her action anında silUrun(Urun urun); methodu çalışacaktır ve oluşturduğumuz listeden tıkladığımız objeyi remove edicektir.
index.xhtml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?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://xmlns.jcp.org/jsf/html" xmlns:f="http://xmlns.jcp.org/jsf/core"> <h:head> <title>www.turkishh.com</title> </h:head> <h:body> <h1>JSF 2 dataTable Örneği</h1> <h:form> <h:dataTable value="#{urunBean.urunList}" var="o" styleClass="urun-table" headerClass="urun-table-header" rowClasses="urun-table-odd-row,order-table-even-row" > <h:column> <f:facet name="header">Sipariş Numarası</f:facet> #{o.siparisNo} </h:column> <h:column> <f:facet name="header">Ürün İsmi</f:facet> #{o.urunIsmi} </h:column> <h:column> <f:facet name="header">Fiyatı</f:facet> #{o.fiyat} </h:column> <h:column> <f:facet name="header">İşlemler</f:facet> <h:commandLink value="Sil" action="#{urunBean.silUrun(o)}" /> </h:column> </h:dataTable> </h:form> </h:body> </html> |
Çıktısı :
İyi Çalışmalar 🙂