프로그래밍/JSP Servlet

JSP/Servlet – 태그클래스를 이용해서 애트리뷰트가 있는 커스텀 액션 만들기

가카리 2016. 2. 23. 21:28
반응형

 

JSP/Servlet – 태그클래스를 이용해서 애트리뷰트가 있는 커스텀 액션 만들기

 

태그클래스를 이용해서 애트리뷰트 값을 받으려면 메소드를 따로 선언해야 합니다.

 

  1. 반드시 public으로 선언
  2. 맨 앞에 set을 붙이고 애트리뷰트 이름의 첫문자를 대문자로
  3. 파라미터 변수를 선언

 

public void setSize(Integer size){

 

}


 

먼저 classes 폴더를 만들고 그 안에 tool 폴더를 만듭니다. 그 다음 태그클래스를 구현하기 위한 NewLineTag.java 파일을 만듭니다.

 

그리고 TLD파일을 만들어서 커스텀 액션을 등록합니다. web.xml파일에는 TLD파일의 URI와 경로를 명시해줍니다. 마지막으로 TagTest.jsp파일을 만들어서

 

커스텀 액션을 테스트합니다.

 

NewLineTag.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 NewLineTag extends SimpleTagSupport{

    private int size;

    private String color;

    

    //size 애트리뷰트를 받음

    public void setSize(Integer size){

        this.size = size;

    }

    //color 애트리뷰르를 받음

    public void setColor(String color){

        this.color = color;

    }

    

    @Override

    public void doTag() throws JspException, IOException {

        JspContext context = getJspContext();

        JspWriter out = context.getOut();

        //폰트의 색을 color 지정후 size 만큼 * 출력

        out.println("<FONT color=" + color + ">");

        for(int cnt = 0; cnt < size; cnt++){

            out.print("*");

        }

        out.println("</FONT><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>newLine</name>

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

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

        <attribute>

            <name>size</name>

            <type>java.lang.Integer</type>

        </attribute>

        <attribute>

            <name>color</name>

            <type>java.lang.String</type>

        </attribute>

    </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_tag_attribute2</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>

 

 

TagTest.jsp

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

pageEncoding="EUC-KR"%>

<%@taglib prefix="tool" uri="/taglibs/tools.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:newLine color="red" size="20"/>

    테스트<BR>

    <tool:newLine color="blue" size="15"/><BR>

</body>

</html>

 

실행 화면

제가 명시한 size 와 color대로 커스텀 액션이 제대로 나옴을 알 수 있습니다.

반응형