JSF – 13 – JSTL , JSTL Functions & JSF

Merhaba arkadaslar.
Bu bolumdeJSP Standard Tag Library (JSTL)  ve JSTL Function’larinin JSF Facelets sayfalarinda kullanimini inceleyecegiz. Bu konulari Servlet&JSP yazilarinda detayli olarak inceledik , bu nedenle burada detayli sekilde incelemeyecegiz.

JSP Standard Tag Library (JSTL) & JSF

JSTL konusunu Servlet&JSP konusunda incelemistik. Burada bir kac ornek yapacagiz ve JSTL kodlarimizi xhtml dosyalarinda kullanabildigimizi gorecegiz.

Facelet dosyalari , <c:out> JSTL tagini kullanmamiza izin vermez!

JSP dosyalarimizda JSTL i kullanabilmek icin @taglib directive eklememiz gerekliydi.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

Facelets sayfalarimizda da namespace eklememiz gerekli

xmlns:c="http://java.sun.com/jstl/core"

<c:set> ,<c:if> gibi JSTL taglari icin jar eklememiz gerekmezken c:forEach icin eklememiz gerekmektedir.

pom.xml

	        <dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.1.2</version>
		</dependency>

		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>1.1.2</version>
		</dependency>

JSTLBean.java

package _17.jstl;

import java.util.HashMap;
import java.util.Map;

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

@ManagedBean(name = "JSTLBean")
@SessionScoped
public class JSTLBean {

	private String property1;
	private Map<String, String> map = new HashMap<String, String>();

        //getters and setters
}


jstlAndFacelets.xhtml

<c:set> JSTL etiketinde var ve target isminde 2 tane farkli attribute vardir.

var versiyonu attribute degiskenlerini eklemek/olusturmak icin kullanilir.
target versiyonu bean property(alanlari) veya Map degerlerini eklemek/olusturmak/atamak icin kullanilir.

<c:if>  , <c:choose> , <c:forEach> gibi taglari kullanabiliriz.

<!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:c="http://java.sun.com/jstl/core">
<h:head>
	<title>Use JSTL in JSF 2.X Facelets</title>
</h:head>
<h:body>

	<!-- c:set tagi ile attribute ekleyebiliriz. -->
	<c:set var="mySessionAttribute" scope="session"
		value="my session value" />
	<!-- c:set tagi ile ekledigimiz attribute'lere ulasalim -->
	<p>${mySessionAttribute}</p>
	<p>#{mySessionAttribute}</p>

	<!-- c:set tagi ile bean property alanlarina deger atayabiliriz. -->
	<p>
		<c:set target="${JSTLBean}" property="property1" value="property1 value" />
	</p>

	<p>${JSTLBean.property1}</p>

	<!-- c:set tagi ile map'lere eleman ekleyebiliriz. -->
	<p>
		<c:set target="${JSTLBean.map}" property="key1" value="map value1" />
	</p>

	<p>${JSTLBean.map.key1}</p>

	
	<c:set var="result" scope="request" value="70" />

	<c:if test="${result > 50}">
		<p>You pass exam ! : ${result}</p>
	</c:if>

	<c:set var="salary" scope="session" value="5000" />

	     <c:choose>
		<c:when test="${salary le 2000}">
	       Low Salary
	    </c:when>

	    <c:when test="${salary lt 10000}">
	        Salary is very good.
	    </c:when>
		<c:otherwise>
	        Excellent salary!
	    </c:otherwise>
	</c:choose>

	<p>
		<c:set target="${JSTLBean.map}" property="key2" value="map value2" />
		<c:set target="${JSTLBean.map}" property="key3" value="map value3" />
		<c:set target="${JSTLBean.map}" property="key4" value="map value4" />
	</p>

	<c:forEach var="entry" items="#{JSTLBean.map}">
		<p>${entry.key} , #{entry.value}</p>
	</c:forEach>


</h:body>
</html>

Ornegimizi calistirdigimizda ;

jstl1

JSTL Functions

JSTL function konusunu da Servlet&JSP yazilarinda ele almistik. Burada hizlica tekrar inceleyelim.

JSF Facelets sayfalarimizda JSTL function’lari kullanabilmek icin namespace ekleyelim ;

xmlns:fn="http://java.sun.com/jsp/jstl/functions"

Burada bir kactane JSTL function kullanimina bakalim. Diger JSTL functionlara suradan bakabilirsiniz;
Servlet & JSP – 29 – JSTL Functions

jstlFunctions.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:h="http://java.sun.com/jsf/html"
	xmlns:c="http://java.sun.com/jstl/core"
	xmlns:fn="http://java.sun.com/jsp/jstl/functions">
<h:head>
	<title>Use JSTL Functions in JSF 2.X Facelets</title>
</h:head>
<h:body>

	<c:set var="myattribute" value="injavawetrust" />

	<c:if test="${fn:contains(myattribute, 'java')}">
		<p>Founded! java</p>
	</c:if>
	
	<c:if test="${fn:containsIgnoreCase(myattribute, 'JAVA')}">
		<p>Founded! JAVA</p>
	</c:if>
	
	<c:set var="myattribute" value="injavawetrust" />

	<c:if test="${fn:startsWith(myattribute, 'in')}">
		<p>String starts with in</p>
	</c:if>


</h:body>
</html>

Ornegimizi calistirdigimizda ;

jstl functions1

Kaynak kodlar : Injavawetrust.jsf

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.