タグのボディを処理するタグライブラリ - JSP

タグのボディ部分も処理するタグライブラリの実装例です。

コード (bodycustom.jsp)

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib uri="/BodyCustomLibrary" prefix="title" %>
<!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=UTF-8">
    <title>Body Custom Library</title>
  </head>
  <body>
    <%-- タイトル文字列を中央揃えテーブル枠・文字サイズ10ptとして表示する--%>
    <title:BodyCustom>
      Body Custom Library
    </title:BodyCustom>
    <BR><HR><BR>
    <P>This page is Body Custom Library</P>
  </body>
</html>

コード (BodyCustom.java)

package webApplication21;

import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;

public class BodyCustom extends TagSupport {

  private PageContext pc = null;
  private JspWriter out = null;

  public void setPageContext(PageContext pc) {
    this.pc = pc;
    this.out = pc.getOut();
  }

  public int doStartTag() throws JspException {
   try {
     //中央揃えテーブル枠としてのテーブルタグと文字サイズ10ptを指定
     out.println("<TABLE align=\"center\" border=\"1\">");
     out.print("<TR><TD><FONT size=\"10\">");
   } catch (Exception e) {
     throw new JspException(e);
   }
   //BODY処理
   return EVAL_BODY_INCLUDE;
  }

  public int doEndTag() throws JspException {
    try {
      //テーブルの終了タグを出力
      out.println("</FONT></TD></TR>");
      out.println("</TABLE>");
    } catch (Exception e) {
      throw new JspException(e);
    }
    //カスタムタグ以降のJSPページを出力
    return EVAL_PAGE;
  }
}

解説

TagSupportクラスから派生したBodyCustomクラスを宣言します。
  public class BodyCustom extends TagSupport {

setPageContext()メソッドを実装します。引数で与えられたPageContextをクラスのメンバ変数に格納します。JspWriterも合わせて取得しメンバ変数に格納します。
 public void setPageContext(PageContext pc) {
   this.pc = pc;
   this.out = pc.getOut();
 }

doStartTag()メソッドを実装します。開始タグで処理するアルゴリズムを実装します。今回の例ではテーブルを開始するテーブルタグを出力します。
  public int doStartTag() throws JspException {
     try {
       //中央揃えテーブル枠としてのテーブルタグと文字サイズ10ptを指定
       out.println("<TABLE align=\"center\" border=\"1\">");
       out.print("<TR><TD><FONT size=\"10\">\");
     } catch (Exception e) {
       throw new JspException(e);
     }
     //BODY処理
     return EVAL_BODY_INCLUDE;
  }

doEndTag()メソッドを実装します。終了タグで処理するアルゴリズムを実装します。今回の例ではテーブルを終了するテーブルタグを出力します。
  public int doEndTag() throws JspException {
    try {
      //テーブルの終了タグを出力
      out.println("</FONT></TD></TR>");
      out.println("</TABLE>");
    } catch (Exception e) {
      throw new JspException(e);
    }
    //カスタムタグ以降のJSPページを出力
    return EVAL_PAGE;
  }

コード (web.xml)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=
    "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  version="2.4">
  <jsp-config>
    <taglib>
      <taglib-uri>/BodyCustomLibrary</taglib-uri>
      <taglib-location>/WEB-INF/tlds/body.tld</taglib-location>
    </taglib>
  </jsp-config>
  <session-config>
    <session-timeout>30</session-timeout>
  </session-config>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

実行結果



実行結果HTML

<!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=UTF-8">
    <title>Body Custom Library</title>
  </head>
  <body>
    <TABLE align="center" border="1">
<TR><TD><FONT size="10">
      Body Custom Library
    </FONT></TD></TR>
</TABLE>
    <BR><HR><BR>
    <P>This page is Body Custom Library</P>
  </body>
</html>
著者
iPentecのプログラマー、最近はAIの積極的な活用にも取り組み中。
とっても恥ずかしがり。
最終更新日: 2024-01-06
作成日: 2011-02-13
iPentec all rights reserverd.