Servlet & JSP – 30 – Tag File

Merhaba Arkadaslar
Bu bolumde Tag File konusunu inceleyecegiz. Tag File <jsp:include> Standard Action’i ya da <c:import> JSTL etiketi gibi calisir. Yani reusable content amaciyla kullanilabilir.

Tag File icin su adimlar yapilmalidir ;

  1. include edilmek istenen dosyanin ismini .tag olarak degistirilmelidir.
    • header.html –> header.tag
  2. tag dosyalari WEB-INF/tags dizininde ya da bu dizinin alt dizinlerinde olmalidir.
  3. JSP dosyasinda taglib directive kullanilarak ilgili tag dosyasi sayfaya include edilir.

header.tag 

<h1>this is header.tag file</h1>

header.tag dosyamiz icinde basit bir h1 etiketi yer almaktadir.

includeTagFile.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%@ taglib prefix="myTags" tagdir="/WEB-INF/tags" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>

	<myTags:header/>

</body>
</html>

Kodu inceleyelim ; oncelikle taglib directive’ini kullandik. prefix attribute alanina diledigimiz bir isim veriyoruz. Su isimleri veremeyiz  , bunlar reserved kelimelerdir.

  • jsp
  • jspx
  • java
  • javax
  • servlet
  • sun
  • sunw

tagdir attribute alani icin header.tag dosyasinin bulundugu dizin bilgisini yazalim. tag dosyalari WEB-INF/tags dizininde ya da alt dizinlerinde olabilir.

JSP dosyamiz icerisinde tag dosyamizi eklemek/include icin <prefixName:tagFilName/> seklinde kullanabiliriz.

<jsp:include> Standard Action icerisinde <jsp:param> ve <c:import> JSTL etiketi icerisinde <c:param> JSTl etiketinin kullanimini incelemistik. Benzer sekilde tag file icerisinde tag attribute kullanabiliriz.

header2.tag

<%@ attribute name="title" required="true" rtexprvalue="true" %>

<h1>${title}</h1>

header2.tag dosyamiz icerisinde @attribute ile tag attribute tanimi yapabiliriz.

  • required attribute alani bu attribute’in kullanilmasinin zorunlu olup olmadigini belirtmek icin kullanilir.
  • rtexprvalue attribute alani , bu attribute’in kullanilirken JSP Expression kullanilip kullanilamayacagini belirtir.

includeTagFile2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%@ taglib prefix="myTags" tagdir="/WEB-INF/tags"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>

	<!-- tag dosyasinda title attribute'u required durumda ! 
	Bu nedenle kullanilmazsa calisma zamaninda hata olur.
	varsayilan degeri required="false" 'tur.
	 -->
	
	<myTags:header2 title="title attribute is required!" />

	<!--tag dosyasinda ,rtexprvalue attribute alani true oldugu icin EL agekullanabiliriz.
	 varsayilan deger rtexprvalue="true"dur.
	  -->
	<%
		request.setAttribute("rtexprvalue", "my runtime expression value");
	%>

	<!-- rtexprvalue="false" olursa burada Expression Language kullanamayiz. -->
	<myTags:header2 title="${rtexprvalue}" />
	
</body>
</html>

Tag file dosyamizi kullanirken body alanini da kullanabiliriz. Bunun icin tag dosyamizda <jsp:doBody/> Standard Action’i eklememiz gereklidir.

header3.tag

<%@ attribute name="title" required="true" rtexprvalue="true" %>


<!--varsayilan olarak body-content scriptless 'tir.  -->
<%@ tag body-content="scriptless" %>

<h1>${title}</h1>


<!-- jsp:doBody Standard Action'i ile body'nin calismasini saglayabiliriz. -->
<strong><jsp:doBody/></strong>

Varsayilan body-content’i scriptless’tir.

        <%
	    request.setAttribute("attribute", "attribute value");
	%>
	
	<%-- header3.tag dosyasi icerisinde <jsp:doBody/> Standard Action'i kullanildigi icin 
	body icerisi calisacaktir. Aksi durumda body icerisi calismaz.
	
	body-content="scriptless" oldugu icin Expression Language degerlendirilir.(evaluated)
	
	body icerisinde hic bir durumda JSP Expression kullanilamaz.
	--%> 
	<myTags:header3 title="header3 title">
		hello body			
		${attribute}
		<%--
	 	<%="Exception !" %>
	 	 --%>
	 </myTags:header3>

header3.tag dosyasi icerisinde Standard Action’i kullanildigi icin body icerisi calisacaktir. Aksi durumda body icerisi calismaz.

body-content=”scriptless” oldugu icin Expression Language degerlendirilir.(evaluated)
body icerisinde hic bir durumda JSP Expression kullanilamaz.

header4.tag

<%@ attribute name="title" required="true" rtexprvalue="true" %>


<%@ tag body-content="tagdependent" %>

<h1>${title}</h1>

<strong><jsp:doBody/></strong>

header4.tag dosyasi icerisinde tag body-content=”tagdependent” kullanildi.
Bu durumda JSP Expression Language degerlendirilmez(not evaluated)

	 <!--header4.tag dosyasi icerisinde   tag body-content="tagdependent"  kullanildi.
	 bu durumda JSP Expression Language degerlendirilmez(not evaluated)
	 -->
	 <myTags:header4 title="header4 title">
		hello body			
		${attribute}	
		<%="Hata vermez. body-content tagdependent oldugu icin bu kismi plain text olarak calistirir."%>
	 </myTags:header4>

header5.tag

<%@ attribute name="title" required="true" rtexprvalue="true" %>

<%@ tag body-content="empty" %>

<h1>${title}</h1>

<strong><jsp:doBody/></strong>

header5.tag dosyasinda body-content empty tanimlandi bu durumda body’e bir sey yazilamaz.Bosluk da koyulamaz!

	 <myTags:header5 title="header5 title"></myTags:header5>
	 <myTags:header5 title="header5 title 2"/>

includeTagFile3.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%@ taglib prefix="myTags" tagdir="/WEB-INF/tags"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>

	<%
		request.setAttribute("attribute", "attribute value");
	%>
	
	<%-- header3.tag dosyasi icerisinde <jsp:doBody/> Standard Action'i kullanildigi icin 
	body icerisi calisacaktir. Aksi durumda body icerisi calismaz.
	
	body-content="scriptless" oldugu icin Expression Language degerlendirilir.(evaluated)
	
	body icerisinde hic bir durumda JSP Expression kullanilamaz.
	--%> 
	<myTags:header3 title="header3 title">
		hello body			
		${attribute}
		<%--
	 	<%="Exception !" %>
	 	 --%>
	 </myTags:header3>
	 
	 
	 <!--header4.tag dosyasi icerisinde   tag body-content="tagdependent"  kullanildi.
	 bu durumda JSP Expression Language degerlendirilmez(not evaluated)
	 -->
	 <myTags:header4 title="header4 title">
		hello body			
		${attribute}	
		<%="Hata vermez. body-content tagdependent oldugu icin bu kismi plain text olarak calistirir."%>
	 </myTags:header4>
	 
	 <!-- header5.tag dosyasinda body-content empty tanimlandi bu durumda body'e bir sey yazilamaz.Bosluk da koyulamaz! -->
	 <myTags:header5 title="header5 title"></myTags:header5>
	 <myTags:header5 title="header5 title 2"/>
		
</body>
</html>

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. Required fields are marked *