2013년 11월 11일 월요일

[JSP/Servlet강좌, 구로 재직자교육]JSP 커스텀 태그예제(JSP Custom Tag)

[JSP/Servlet강좌, 구로 재직자교육]JSP 커스텀 태그예제(JSP Custom Tag)

JSP Custom Tag 사용하기 위해서는 3가지의 component 만들어야 하는데 Tag 행동을 정의하기 위한 tag handler, Tag구현과 XML요소를 mapping 하기 위한 tag library, tag library 사용하기 위한 JSP 페이지 등이 필요 합니다.
 
아래 예제를 참고하세요
////////////// here is very simple JSP tag that just inserts a string 
////////////// into the output
package test;
public class ExampleTag extends TagSupport {
  public int doStartTag() {
    try {
      JspWriter out = pageContext.getOut();
      out.print("Custom tag example " + getClass().getName());
    catch (IOException ioe) {
      System.out.println("Error in ExampleTag: " + ioe);
    }
    return (SKIP_BODY);
  }
}
////////////// here is a tag library descriptor (test.tld):
<?xml version="1.0"?>
<! DOCTYPE taglib 
  PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
  "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
   
<taglib>
  <tlibversion>1.0</tlibversion>
  <jspversion>1.1</jspversion>
  <shortname>csajsp</shortname>
  <urn></urn>
  <info>A sample tags library</info>
  <tag>
    <name>example</name>
    <tagclass>test.ExampleTag</tagclass>
    <info>Simplest example: inserts one line of output</info>
    <bodycontent>EMPTY</bodycontent>
  </tag>
  <!-- Other tags defined later... -->
</taglib>
////////////// here is a page using our custom JSP tag :
<%@page language="java"%>
<%@ taglib uri="test.tld" prefix="test" %>
<html>
  <head>
          <title><test:example /></title>
  </head>
  <body>
    <test:example />
  </body>
</html>

 

댓글 없음:

댓글 쓰기