프로그래밍/JSP Servlet

JSP/Servlet – SimpleTagSupport 클래스를 이용해서 태그 클래스 작성하기

가카리 2016. 2. 22. 22:00
반응형

 

JSP/Servlet – SimpleTagSupport 클래스를 이용해서 태그 클래스 작성하기

 

SimpleTagSupport 클래스는 태그 클래스라고 해서 커스텀 액션을 만드는데 사용됩니다.

 

서블릿 대신 JSP로 구현하듯이 태그 파일 대신에 태그 클래스로 작성할 수 있습니다.

 

SimpleTagSupport 클래스를 이용해서 태그 클래스를 만들때 작성해야하는 메소드는 doTag 메소드 하나뿐 입니다.

 

doTag 클래스를 작성할 때는

  1. public으로 선언해야합니다.    
  2. 파라미터가 없어야합니다.
  3. JspException, IOException을 던져야합니다.

 

이 3가지를 반드시 지켜야 합니다.

 

태그 클래스안에서 출력을 위해서는 getJspContext 메소드를 호출해서 JspContext 객체를 가져옵니다.

 

그 다음 getOut메소드를 이용해서 JspWriter객체를 가져오면 HTML로 출력할 수 있습니다.

 

    JspContext context = getJspContext();

    JspWriter out = context.getOut();

 

 

예제는 위와 같이 구성됩니다. 맨 처음 WEB-INF아래에 classes 폴더를 만들고 그 아래에 tool 폴더를 만듭니다. 그리고 태그 클래스를 구현하는 부분인

 

StarLineTag.java 파일을 만듭니다. 태그 클래스 작성이 끝나면 커스텀 액션을 등록하기 위해 TLD 파일을 만듭니다.

 

등록도 끝나면 web.xml 파일에 TLD파일을 등록해야 합니다. 그래야만 TLD 파일을 식별하고 jsp파일에서 커스텀 액션을 사용할 수 있습니다.

 

StarLineTag.java

 

package tool;

 

import java.io.IOException;

 

import javax.servlet.jsp.JspContext;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.JspWriter;

import javax.servlet.jsp.tagext.SimpleTagSupport;

 

public class StarLineTag extends SimpleTagSupport{

        @Override

        public void doTag() throws JspException, IOException {

            JspContext context = getJspContext();

            JspWriter out = context.getOut();

            out.println("-*-*-*-*-*-*-*<BR>");

            return;

        }

    

}

 

 

tools.tld

 

<taglib xmlns="http://java.sun.com/xml/ns/javaee" version="2.1">

    <tlib-version>1.0</tlib-version>

    <short-name>tool</short-name>

    <tag>

        <name>starLine</name>

        <tag-class>tool.StarLineTag</tag-class>

        <body-content>empty</body-content>

    </tag>

</taglib>

 

 

web.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

<display-name>ch10_jsp_simpletagsupport</display-name>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>

    <jsp-config>

         <taglib>

         <taglib-uri>/taglibs/tools.tld</taglib-uri>

         <taglib-location>/WEB-INF/tlds/tools.tld</taglib-location>

        </taglib>

    </jsp-config>

</web-app>

 

StarLineTest.jsp

 

<%@ page language="java" contentType="text/html; charset=EUC-KR"

pageEncoding="EUC-KR"%>

<%@taglib prefix="tool" uri="/taglibs/tools.tld" %><!-- TLD파일을 가져오는 -->

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">

<title>Insert title here</title>

</head>

<body>

    <tool:starLine/>

    테스트입니다. <BR>

    <tool:starLine/>

</body>

</html>

 

 

실행 화면

다음과 같이 태그클래스로 만든 커스텀 액션이 정상적으로 작동함을 알 수 있습니다.

 

반응형