Merhabalar arkadaşlar. Bu yazımızda JSF’de setpropertyActionListener’ı ve attribute’u göreceğiz. Yapacağımız örneğin kaynak kodları aşağıdadır ve kodların açıklamaları ilgili kodun altındadır. Bu örnekte sayfada 2 textBox,1 + butonu ve 1 – butonu bulunacak,butonlara tıklandığında gideceği sayfa aynı olacak,sayfada tıklanılan butona göre gerekli işlem yapılıp sonuç yazdırılacak. Yani bunu bir nevi basit bir hesap makinesi gibi düşünebilirsiniz. Kodu geliştirip diğer işlemleri de koyduğunuzda çalışan bir hesap makinesine dönüştürebilirsiniz.
SetPropertyActionListener
index.xhtml Kodu
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 |
<?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:body> <h1>JSF 2 setPropertyActionListener example</h1> <h:form id="form"> <h:inputText value="#{user.ilk}"/> <h:inputText value="#{user.son}"/> <h:commandButton action="#{user.outcome}" id="topla" value="+" actionListener="#{user.hesapla}"> <f:setPropertyActionListener target="#{user.islem}" value="+" /> </h:commandButton> <h:commandButton action="#{user.outcome}" id="cikar" value="-" actionListener="#{user.hesapla}"> <f:setPropertyActionListener target="#{user.islem}" value="-" /> </h:commandButton> </h:form> </h:body> </html> |
Önceki dersten de hatırlayacağımız gibi bir butona actionListener özelliğini verip ona bir olay metodu tanımlayabiliyorduk. index kodu içinde de göreceğiniz üzere “+” butonuna bir bir olay tanımladık fakat bu örnekte fazladan bir de <f:setPropertyActionListener> kullandık. setPropertyActionListener bizim Java tarafındaki nesnenin değerini anlık değiştirmemiz sağlayan sağlayan etikettir. Bu etiket içinde target bizim değeri hangi nesneye geçireceğimizi belirleyen özelliktir. value da değer olarak ne geçireceğimizi belirtir. + butonuna tıklayınca toplama işlemini yaptıracağız. Aynıları – butonu için de geçerli.
result.xhtml Kodu
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?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:body> <h1>JSF 2 setPropertyActionListener example</h1> #{user.ilk}#{user.islem}#{user.son}= #{user.sonuc} </h:body> </html> |
Bu kodda girilen ilk sayı,işlem,ikinci sayı= sonuc şeklinde yapılan işlemi yazdırıyoruz.
UserBean.java Kodu
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 46 47 48 49 50 51 52 53 54 55 56 |
import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import javax.faces.event.ActionEvent; @ManagedBean(name="user") @SessionScoped public class UserBean{ public String islem; public String ilk; public String son; public String sonuc; int a; int b; int c; public String outcome(){ return "result"; } public String getislem() { return islem; } public void setislem(String islem) { this.islem = islem; } public void setilk(String ilk) { this.ilk=ilk; } public String getilk() { return ilk; } public void setson(String son) { this.son=son; } public String getson() { return son; } public String getsonuc() { return sonuc; } public void hesapla(ActionEvent e) { if ((e.getComponent().getId()).equals("topla")){ a=Integer.parseInt(ilk); b=Integer.parseInt(son); c=a+b; } else{ a=Integer.parseInt(ilk); b=Integer.parseInt(son); c=a-b; } sonuc= String.valueOf(c); } } |
Java kodumuzda olay metodumuz tıklanılan butonun ID bilgisine göre gerekli işlemi gerçekleştirme. Örneğin tıklanılan buton + ise toplama işlemi – ise çıkarma işlemi yapılacak.
Ekran çıktılarımıza da bir bakalım.
Attribute
<f:attribute> etiketi gerçek anlamda parametre geçirmemiz sağlayan etikettir. Bu etiketin kulanımı için index.xhtml kodunu şu şekilde değiştirmemiz gerekir:
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 |
<?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:body> <h1>JSF 2 setPropertyActionListener example</h1> <h:form id="form"> <h:commandButton action="#{user.outcome}" id="topla" value="+" actionListener="#{user.hesapla}"> <f:attribute name="sayi1" value="5"/> <f:attribute name="sayi2" value="5"/> <f:setPropertyActionListener target="#{user.islem}" value="+" /> </h:commandButton> <h:commandButton action="#{user.outcome}" id="cikar" value="-" actionListener="#{user.hesapla}"> <f:attribute name="sayi1" value="5"/> <f:attribute name="sayi2" value="5"/> <f:setPropertyActionListener target="#{user.islem}" value="-" /> </h:commandButton> </h:form> </h:body> </html> |
f:attribute etiketinde geçireceğimiz parametreye bir isim veriyoruz ki Java tarafında bu isim ile işlem yapacağız. value ile de geçirilecek değeri bildiriyoruz.
Java kodumuzu da şu şekilde değiştirelim:
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import javax.faces.event.ActionEvent; @ManagedBean(name="user") @SessionScoped public class UserBean{ public String islem; public String ilk; public String son; public String sonuc; int a; int b; int c; public String outcome(){ return "result"; } public String getislem() { return islem; } public void setislem(String islem) { this.islem = islem; } public void setilk(String ilk) { this.ilk=ilk; } public String getilk() { return ilk; } public void setson(String son) { this.son=son; } public String getson() { return son; } public String getsonuc() { return sonuc; } public void hesapla(ActionEvent e) { if ((e.getComponent().getId()).equals("topla")){ ilk=(String)e.getComponent().getAttributes().get("sayi1"); son=(String)e.getComponent().getAttributes().get("sayi2"); a=Integer.parseInt(ilk); b=Integer.parseInt(son); c=a+b; } else{ ilk=(String)e.getComponent().getAttributes().get("sayi1"); son=(String)e.getComponent().getAttributes().get("sayi2"); a=Integer.parseInt(ilk); b=Integer.parseInt(son); c=a-b; } sonuc= String.valueOf(c); } } |
f:attribute ile geçirdiğimiz parametreyi Java tarafında ismi ile çekip işlem yaptık.
Bu yazımızda da bu kadar arkadaşlar. Özetleyecek olursak actionListener bir sayfadaki bilgiyi gelecek işleme göre göstermemizi sağlayan yapıdır.Bu örnekte +’ya basınca toplama işlemi -‘ye basınca çıkarma işlemi yaptı ve <f:setPropertyActionListener> result sayfasında yapılan işlemi göstermemizi sağladı.
Gelecek yazımızda veri tabanından Apache Shiro ile denetim yaptırmayı göreceğiz. Esen kalın.