CAFE

JSP

몸체 내용이 없는 커스텀 태그 구현

작성자새우깡쥐머리|작성시간10.02.12|조회수125 목록 댓글 0

----------------------------------------class 파일 ---------------------------------------------------------------

package com.test;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.*;

<!--tag  interface와 IterationTag interface를 상속받은 TagSupprt를 상속-->

public class HeaderListTag extends TagSupport{   

 public int doEndTag() throws JspException {
  //나머지 jsp도 실행
  return EVAL‎_PAGE;  
 }

 @Override
 public int doStartTag() throws JspException {
  JspWriter out = pageContext.getOut(); 

   //pageContext는 TagSupport에 정의되어 있는 필드로 커스텀 태그를 사용하는 jsp의 기본객체와 동일

  HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
  try{
   out.println("<ol>");
   Enumeration en = request.getHeaderNames();
   while(en.hasMoreElements())
   {
    String name = (String)en.nextElement();
    out.println("<li>"+name+"="+request.getHeader(name)+"<li>");
   }
   out.println("</ol>");
  }catch(IOException e)
  {
   throw new JspException(e);
  }
  // TODO Auto-generated method stub
  //바디부분을 건너뛰고 실행
  return SKIP_BODY;
 } 
}

---------------------------------------- tld 파일(classicStyle.tld) --------------------------------------------------

<?xml version="1.0" encoding="euc-kr"?>

<taglib xmlns="http://java.sun.com/xml/ns/javaee"    <!--web.xml설정과 동일하게 맞출것: parisng에러발생 -->
        xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance
        xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        id="WebApp_ID" version="2.5"
>
        <description>클래식 스타일의 커스텀 태그 예제</description>
        <display-name>클래식 스타일의 예제</display-name>
        <tlib-version>1.0</tlib-version>  <!--테그 라이브러리 버젼(필수속성)-->
        <short-name>classStyle</short-name><!--태그 라이브러리의 이름(필수 속성)-->
        <uri>http://test.com/study/classTag</uri> <!--라이브러리 버젼 구분 사용 URI(옵션)-->
        
        <tag>
         <name>headerList</name>   <!--태그 이름-->
         <tag-class>com.test.HeaderListTag</tag-class> <!--클래스 위치 -->
         <body-content>empty</body-content>  <!--body가 없을때는 empty로 선언-->
        </tag>

</taglib>

 

---------------------------------------- web.xml파일 (추가) -------------------------------------------------------

  <taglib>
   <taglib-uri>http://test.com/study/classTag</taglib-uri> <!--라이브러리 버젼 구분 사용 URI-->
   <taglib-location>/WEB-INF/tlds/classicStyle.tld</taglib-location><!--tld파일의 위치-->
  </taglib>

-----------------------------------------jsp파일 ------------------------------------------------------------------

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

<%@taglib prefix="exam" uri="http://test.com/study/classTag"%>
<exam:headerList/>

 

다음검색
현재 게시글 추가 기능 열기

댓글

댓글 리스트
맨위로

카페 검색

카페 검색어 입력폼