JSF – 02 – GlassFish Server & JSF

Merhaba Arkadaslar,
Bu bolumde Glassfish Application Server ‘in Eclipse Luna entegrasyonunu ve CDI Bean’i incelecegiz.

Oncelikle gerekli dosyamizi indirelim  ve uygun bir yere zip dosyamizi cikartalim.
GlassFish Server , http://glassfish.java.net/download.html

Java EE 7 Web Profile , JSF ve CDI beanleriyle calistigimizda yeterli olacaktir.

glassfisf versions

Eclipse Luna versiyonunda ” New Server Runtime Environment ” menusunde varsayilan olarak Glassfish Application Server’i yoktur. Bunun icin oncelikle Glassfish Server Adapter kurmamiz gereklidir.

Eclipse Market Place’ten bu islemi gerceklestirebiliriz . Arama menusune glassfish yazdigimizda ilgili Glassfish Tools gelecektir. Ben de suan kurulu oldugu icin install secenegi yok siz buradan kurabilirsiniz.

glassfish tool

Kurulum tamamlandiktan sonra Eclipse yeniden baslatilacaktir. Artik New Server Runtime Environment menusunde  Glassfish de mevcut olacaktir.

new server runtime environment

Bir sonraki menude ( New Server menusu)  Server root dizini gostermemiz gereklidir.
Burada dikkat etmemiz gereken glassfish4 altinda glassfish dizinini gostermemiz gerekmektedir. Finish diyip Glassfish Application Server’i ekleyebiliriz.

new server

Glassfish de Apache Tomcat ve Oracle gibi 8080 portunu varsayilan olarak kullanir.
8080 portu kullanimdaysa veya portu degistirmek istersek su yolu takip edebiliriz ;

glassfish4---> glassfish --> domains --> domain1--> config-- > domain.xml

dolphin

domain.xml dosyamizda portla ilgili degisiklikleri su kisimdan yapabiliriz.

        <network-listeners>
          <network-listener port="8080" protocol="http-listener-1" transport="tcp" name="http-listener-1" thread-pool="http-thread-pool"></network-listener>
          <network-listener port="8181" protocol="http-listener-2" transport="tcp" name="http-listener-2" thread-pool="http-thread-pool"></network-listener>
          <network-listener port="4848" protocol="admin-listener" transport="tcp" name="admin-listener" thread-pool="admin-thread-pool"></network-listener>
        </network-listeners>

CDI Beans

Context and Dependency Injection , JSF ManagedBean’lerden daha esnek ozelliklere sahiptir.

CDI beanleri Application Server uzerinde calisir. Apache Tomcat Servlet Container ozelligine sahiptir. Glassfish hem CDI beanleri hem ManagedBean’leri destekler. CDI beanleri Tomcat tarafindan desteklenmez. CDI beanlerin desteklenmesi icin reference implementation Apache Tomcat’e eklenebilir.

  1. @ManagedBean yerine @Named annotation kullanabiliriz.
    Bu durumda javax.faces.bean.ManagedBean yerine javax.inject.Named sinifini kullaniriz.
  2. javax.faces.bean.SessionScoped;
    yerine javax.enterprise.context.SessionScoped; kullanmamiz gereklidir.
  3. @Named annotation kullandigimizda Serializable arabirimini implements etmemiz gereklidir.

Oncelikle pom.xml dosyamiza gerekli jarlari eklememiz gereklidir. Onceki bolumde jsf-api ve jsf-impl dependency tanimlarini eklemistik. BUrada javax.inject ve cdi-api dependency tanimlarini ekleyelim.

pom.xml

<dependencies>
	<dependency>
		<groupId>com.sun.faces</groupId>
		<artifactId>jsf-api</artifactId>
		<version>2.2.13</version>
	</dependency>
	<dependency>
		<groupId>com.sun.faces</groupId>
		<artifactId>jsf-impl</artifactId>
		<version>2.2.13</version>
	</dependency>

	<dependency>
		<groupId>javax.inject</groupId>
		<artifactId>javax.inject</artifactId>
		<version>1</version>
	</dependency>

	<dependency>
		<groupId>javax.enterprise</groupId>
		<artifactId>cdi-api</artifactId>
		<version>1.2</version>
	</dependency>

</dependencies>
		

HelloGlassFishBean.java

package _02.helloglassfish;

import java.io.Serializable;

import javax.inject.Named;
//import javax.faces.bean.ManagedBean;

// @ManagedBean yerine @Named annotation kullanabiliriz.

// javax.faces.bean.ManagedBean yerine javax.inject.Named kullanabiliriz.

//import javax.faces.bean.SessionScoped;
// @Named kullandigimizda 
// javax.faces.bean.SessionScoped; yerine javax.enterprise.context.SessionScoped; kullanmamiz gereklidir.

import javax.enterprise.context.SessionScoped;

@Named(value = "glassfishBean")
@SessionScoped
// @ManagedBean
// @Named annotation kullanildiginda Serializable arabirimi implements
// edilmelidir.
public class HelloGlassFishBean implements Serializable {

	private String name;
	private String password;

	public String getName() {
		return name;
	}

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

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}

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">
<h:head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>Welcome</title>
</h:head>
<h:body>
	<h:form>
		<h3>Please enter your name and password.</h3>
		<table>
			<tr>
				<td>Name:</td>
				<td><h:inputText value="#{glassfishBean.name}" /></td>
			</tr>
			<tr>
				<td>Password:</td>
				<td><h:inputSecret value="#{glassfishBean.password}" /></td>
			</tr>
		</table>
		<p>
			<h:commandButton value="Login" action="welcome" />
		</p>
	</h:form>
</h:body>
</html>

welcome.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>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>Welcome</title>
</h:head>
<h:body>
	<h3>Welcome to www.injavawetrust.com JSF tutorial!
		#{glassfishBean.name}</h3>
</h:body>
</html>

Run Application

login.xhtml dosyamizi Glassfish Server uzerinde calistiralim.

login glassfish

welcome glassfish

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.