Servlet & JSP – 35 – Classic Tag Handler – 03

Merhaba arkadaslar,
Bu bolumde BodyTagSupport sinifini inceleyecegiz. Onceki bolumde Tag Handler API icin bir sinif/interface hiyerarsini incelemistik.

Suana kadar Classic Tag olarak TagSupport sinifindan kalitim yaparak kullandik. Bununla birlikte BodyTag arabirimi ve bu arabirimi uygulayan BodyTagSupport sinifini kullanmadik. BodyTagSupport sinifi ayni zamanda TagSupport sinifi da kalitmaktadir.

Dolayisiyla su onermerlerin hepsi dogru olacaktir ;

BodyTagSupport IS-A TagSupport
BodyTagSupport IS-A IterationTag
BodyTagSupport IS-A BodyTag
BodyTagSupport IS-A Tag
BodyTagSupport IS-A JspTag

butun bu siniflar ve arabirimler javax.servlet.jsp.tagext  paketinde yer almaktadir.

TagSupport sinifi doStartTag metodu 2 donus degerine sahipti ;

SKIP_BODY (varsayilan/default return value)
EVAL_BODY_INCLUDE

TagSupport.class

    @Override
    public int doStartTag() throws JspException {
        return SKIP_BODY;
    }

BodyTagSupport sinifi icin doStartTag metodu 3 donus degeri mevcuttur

EVAL_BODY_BUFFERED(varsayilan/default return value)
SKIP_BODY
EVAL_BODY_INCLUDE

BodyTagSupport.class

    @Override
    public int doStartTag() throws JspException {
        return EVAL_BODY_BUFFERED;
    }

BodyTagSupport sinifinda doInitBody metodu yer almaktadir. Bu metot body calismadan once bir kez calisir. Varsayilan olarak NOOP durumundadir.

    @Override
    public void doInitBody() throws JspException {
        // NOOP by default
    }

doInitBody metodunda out.print metodu direkt cikti olusturmaz bunu bodyContent olarak tamponlar.(buffer) Benzer sekilde bodyContent.print metodunu da kullanabiliriz.

doInitBody metodunun calismasi icin doStartTag metodu EVAL_BODY_BUFFERED donus degerine sahip olmalidir.

BodyTagSupport.class

protected transient BodyContent bodyContent;

ClassicTagHandler7.java

package _15_ClassicTagHandler;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class ClassicTagHandler7 extends BodyTagSupport {

	@Override
	public int doStartTag() throws JspException {

		JspWriter out = pageContext.getOut();
		try {
			out.print("<p> doStartTag method works ! </p>");
		} catch (IOException e) {
			e.printStackTrace();
		}
		// doInitBody metodunun calismasi icin EVAL_BODY_BUFFERED donmelidir!
		return EVAL_BODY_BUFFERED;
	}

	@Override
	public void doInitBody() throws JspException {

		JspWriter out = pageContext.getOut();
		try {
			// doInitBody metodunda out.print metodu direkt cikti olusturmaz
			// bunu bodyContent olarak tamponlar.(buffer)
			out.print("<p> doInitBody out.print works ! </p>");
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			// Benzer sekilde bodyContent.print metodunu da kullanabiliriz.
			bodyContent.print("<p> doInitBody bodyContent.print ! </p>");
		} catch (IOException e) {
		}

	}

	public int doAfterBody() throws JspException {

		JspWriter out = pageContext.getOut();
		try {
			out.print("<p> doAfterBody out print works ! </p>");
		} catch (IOException e) {

			e.printStackTrace();
		}

		try {
			bodyContent.print("<p> doAfterBody bodyContent.print works ! </p>");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return SKIP_BODY;

	}

	public int doEndTag() throws JspException {
		try {
			System.out.println(bodyContent.getString());
			// tamponlanan(buffered) icerigi doEndTag metodunda yazabiliriz.
			pageContext.getOut().print(bodyContent.getString());

			pageContext.getOut().print("<p> doEndTag finished! </p>");
			return EVAL_PAGE;
		} catch (IOException e) {
			throw new JspException(e.getMessage());
		}
	}

}

classicTag.tld

	<tag>
		<name>myClassicTagName7</name>
		<tag-class>_15_ClassicTagHandler.ClassicTagHandler7</tag-class>
		<body-content>tagdependent</body-content>
	</tag>

classicTag7.jsp

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

<%@ taglib uri="myClassicTagURI" prefix="myClassicTags7"%>

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

	<myClassicTags7:myClassicTagName7>
		<p>This is classic tag body.</p>
	</myClassicTags7:myClassicTagName7>

	<p>after classic tag works</p>

</body>
</html>

Ornegimizi calistiralim ;

bodytagsupport example

tamponlanan(buffered) bodyContent bilgisini console’da su sekilde gorebiliriz

	System.out.println(bodyContent.getString());
<p> doInitBody out.print works ! </p><p> doInitBody bodyContent.print ! </p>
		<p>This is classic tag body.</p>
	<p> doAfterBody out print works ! </p><p> doAfterBody bodyContent.print works ! </p>

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 *