JSF MANAGED

JSF Managed Bean ile Ajax() kullanmasına örnek vermeye çalıştım. İnput alanına bir deger girip button tıklandığında ajax ile diğer sayfayı çağırıyor ve o sayfada ekrana yazıyor. Bu şöyle oluyor. Button tıklandığında girilen deger KulBean’e kayedilir ve ajax ile çağrılan sayfada KulBean’den kayedilen değer alınıp ekrana yazılır. JSF Hibernate örneğine bakmak için tıklayın.

JSF’de Managed Bean, sayfalar arasında veri taşımayı(cacheliyor yani) sağlar. Bu örnekte asıl amaç JSF 2.0 ile gelen bir-iki kolaylığı göstermektir. JSF1.x ile Managed Bean tanımlamak için JSF Faces Configuration(faces-config.xml) içine mutlaka aşağıdaki gibi bir tanımlama yapman gerekiyordu.

1.
faces-config.xml

<managed-bean>
    <managed-bean-name>kulBean</managed-bean-name>
    <managed-bean-class>com.ethemsulan.KulBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

JSF 2.0 da bunu yazmak zorunda değilsin. Beanimizin başına aşağıdaki gibi bir annotation ile managed bean olduğunu belirtmiş oluyoruz.

@ManagedBean
@RequestScoped

2.
index.xhtml sayfasından KulSayfasi.xhtml sayfasına navigasyon için(mesela girilen degeri diğer sayfaya aktarmak bean ile) faces-config.xml içine aşağıdaki kodu yazmak gerekiyordu.
faces-config.xml

<navigation-rule>
    <from-view-id>index.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>KulSayfasi</from-outcome>
        <to-view-id>/KulSayfasi.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>

Ama JSF 2.0 ile aşağıdaki gibi sayafa ismi ile navigasyonu gerçekleştirebiliriz. Button tıklandığında action=’hangiSayfaIsmi’ yazilmişsa o sayfaya gider.

<h:commandButton action="KulSayfasi" value="Gonder"/>

index.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>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form>
            <h:inputText id="p" value="#{kulBean.property}"/>
            <h:commandButton action="KulSayfasi" value="Gonder">
                <f:ajax execute="p"/>
            </h:commandButton>
        </h:form>
    </h:body>
</html>

KulSayfasi.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>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        Ajax ile gonderilen deger:<h:outputText value="#{kulBean.property}"/>
    </h:body>
</html>

KulBean.java – JSF Managed Bean

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package com.ethemsulan;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

/**
*
* @author ubuntu
*/
@ManagedBean
@RequestScoped
public class KulBean implements Serializable{

/** Creates a new instance of KulBean */
public KulBean() {
}
public String property;

public String getProperty() {
return property;
}

public void setProperty(String property) {
this.property = property;

Bu örnek bu kadardı ama göstermek içinde faces-config.xml dosyasında nasıl tanımlandığına bakabilirsiniz. Bu .xml dosyasının arayüzü de var ve oradan da düzenleyebilirsiniz. Zaten New->Other->JavaServerFaces den JSF Managed Bean seçildiğinde @ManagedBean’i ekliyor. Normal java classını managed bean yapmak için ise sınıfın başına
import javax.faces.bean.*; ile ekleyin ve @ yazıp ctr +space basınca bir sürü annotation çıkıyor
faces-config.xml

<?xml version='1.0' encoding='UTF-8'?>

<!-- =========== FULL CONFIGURATION FILE ================================== -->

<faces-config version="2.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">

<managed-bean>
    <managed-bean-name>kulBean</managed-bean-name>
    <managed-bean-class>com.ethemsulan.KulBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<navigation-rule>
    <from-view-id>index.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>KulSayfasi</from-outcome>
        <to-view-id>/KulSayfasi.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>
</faces-config>