Servlet & JSP – 37 – Nested Tags

Merhaba Arkadaslar.
Bu bolumde Nested Tag konusunu inceleyecegiz. Bu bolumle birlikte Custom Tag konusu son bulacak. Nested Tag yapisina ornek olarak <c:choose> <c:when> <c:otherwise> ornek verilebilir.

Simple Tag , hem Simple Tag hem de Classic Tag parent’a sahip olabilir.
Classic Tag ise Classic Tag parent’a sahip olabilir.

Simple Tag icin getParent() metodu JspTag donus tipine sahiptir.

SimpleTagSupport.java

@Override
public JspTag getParent() {
     return this.parentTag;
}

Classic Tag icin getParent() metodu Tag donus tipine sahiptir.

TagSupport.java

@Override
public Tag getParent() {
     return parent;
}

Tag interface’si JspTag interface’sini kalitmaktadir.

jsptag and tag interface

NestedSimpleTagHandler.java

package _17.NestedTagHandler;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspTag;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;


public class NestedSimpleTagHandler extends SimpleTagSupport {

	private int tagLevel = 0;

	@Override
	public void doTag() throws JspException, IOException {
			
		JspTag parent =  getParent();
		while (parent != null) {
			parent = ((SimpleTagSupport) parent).getParent();
			tagLevel++;
		}	
		getJspContext().getOut().print("<br>Nested level: "+tagLevel);
		getJspBody().invoke(null);
		
	}
}

nestedTag.tld

	<tag>
		<name>mySimpleNestedTagName</name>
		<tag-class>_17.NestedTagHandler.NestedSimpleTagHandler</tag-class>
		<body-content>scriptless</body-content>
	</tag>

simpleNestedTag.jsp

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

<%@ taglib uri="nestedTagURI" prefix="myTags2"%>

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

	<myTags2:mySimpleNestedTagName>
		<myTags2:mySimpleNestedTagName>
			<myTags2:mySimpleNestedTagName>
			</myTags2:mySimpleNestedTagName>
		</myTags2:mySimpleNestedTagName>
	</myTags2:mySimpleNestedTagName>

</body>
</html>

simple nested tag

Benzer ornegi Classic Tag ile de yapabiliriz.

NestedClassicTagHandler.java

package _17.NestedTagHandler;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspTag;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

@SuppressWarnings("unused")
public class NestedClassicTagHandler extends TagSupport {

	private int tagLevel = 0;

	public int doStartTag() throws JspException {
		tagLevel = 0;
		Tag parent = getParent();
		while (parent != null) {
			parent = parent.getParent();
			tagLevel++;
		}

		try {
			pageContext.getOut().println("<br>Nested level: " + tagLevel);
		} catch (IOException e) {
			e.printStackTrace();
		}

		return EVAL_BODY_INCLUDE;

	}
}

nestedTag.tld

<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-
jsptaglibrary_2_0.xsd"
	version="2.0">

	<uri>nestedTagURI</uri>

	<tlib-version>2.0</tlib-version>

	<tag>
		<name>mySimpleNestedTagName</name>
		<tag-class>_17.NestedTagHandler.NestedSimpleTagHandler</tag-class>
		<body-content>scriptless</body-content>
	</tag>

	<tag>
		<name>myClassicNestedTagName</name>
		<tag-class>_17.NestedTagHandler.NestedClassicTagHandler</tag-class>
		<body-content>scriptless</body-content>
	</tag>

</taglib>

classicNestedTag.jsp

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

<%@ taglib uri="nestedTagURI" prefix="myTags2"%>

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

	<myTags2:myClassicNestedTagName>
		<myTags2:myClassicNestedTagName>
			<myTags2:myClassicNestedTagName>
			</myTags2:myClassicNestedTagName>
		</myTags2:myClassicNestedTagName>
	</myTags2:myClassicNestedTagName>

</body>
</html>

classic nested tag

getParent() metodu olmasina ragmen getChild() diye bir metot yoktur.Eger Parent tag Child tag’da yer alan bilgilere ihtiyac duyuyorsa bu durumda Child tag’a ufak bir metot eklemek cozum olacaktir.

Menu.java

package _17.NestedTagHandler;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class Menu extends TagSupport {

	private ArrayList items;

	// dikkat edelim attribute setter metodu degil!
	// bu metot MenuItem sinifinda cagrilmaktadir.
	// Child parent'taki item degerini ArrayList'e ekliyoruz.
	// Boylelikle Parent Tag , Child Tag'in attribute degerine ulasabilir.
	public void addMenuItem(String item) {
		items.add(item);
	}

	public int doStartTag() throws JspException {
		// classic tag larda instance variable'i resetlemeyi unutmayalim!
		items = new ArrayList();
		// donus tipi EVAL_BODY_INCLUDE olmali ki Child Tag calissin.
		return EVAL_BODY_INCLUDE;
	}

	public int doEndTag() throws JspException {

		try {
			pageContext.getOut().println("Menu items are: " + items);
		} catch (IOException e) {
			e.printStackTrace();
		}

		return EVAL_PAGE;

	}
}

MenuItem.java

package _17.NestedTagHandler;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class MenuItem extends TagSupport {
	
	//MenuItem Tag'i icin  TLD dosyamizda itemValue attribute ekledik.
	private String itemValue;

	public void setItemValue(String value) {
		itemValue = value;
	}

	public int doStartTag() throws JspException {
		return EVAL_BODY_INCLUDE;
	}

	public int doEndTag() throws JspException {
		Menu parent = (Menu) getParent();
		parent.addMenuItem(itemValue);
		return EVAL_PAGE;
	}
}

nestedTag.tld

	<tag>
		<name>menu</name>
		<tag-class>_17.NestedTagHandler.Menu</tag-class>
		<body-content>scriptless</body-content>
	</tag>

	<tag>
		<name>menuItem</name>
		<tag-class>_17.NestedTagHandler.MenuItem</tag-class>
		<body-content>scriptless</body-content>
		<attribute>
			<name>itemValue</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
	</tag>

getChildAttribute.jsp

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

<%@ taglib uri="nestedTagURI" prefix="myTag"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Get Child Attribute From Parent</title>
</head>
<body>

	<myTag:menu>
		<myTag:menuItem itemValue="Servlet" />
		<myTag:menuItem itemValue="JSP" />
		<myTag:menuItem itemValue="JSF" />
	</myTag:menu>

</body>
</html>

get child attribute

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.