タグライブラリの利用例 (属性値でフォントサイズを指定する) - JSP

属性値を指定できるタグライブラリのコードを紹介します。

コード (getsize.jsp)

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/SizeLibrary" prefix="sl" %>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
  </head>
  <body>
    <h1>JSP Page</h1>
    <sl:getSize fontSize="8">てすとだよ~</sl:getSize>
  </body>
</html>

コード (size.tld)

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" xmlns="http://java.sun.com/xml/ns/javaee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">

  <tlib-version>1.0</tlib-version>
  <short-name>size</short-name>
  <uri>/WEB-INF/tlds/size</uri>
  <tag>
    <name>getSize</name>
    <tag-class>webApplication24.GetSizeHandler</tag-class>
    <attribute>
      <!--フォントサイズを設定する属性-->
      <!--属性を必ず指定しなくてはならない-->
      <name>fontSize</name>
      <required>true</required>
    </attribute>
  </tag>
</taglib>

コード (GetSizeHandler.java)

package webApplication24;

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

public class GetSizeHandler extends TagSupport {

  private PageContext pc = null;
  private JspWriter out = null;
  private int fontSize = 0;

  /** Creates a new instance of GetSizeHandler */
  public GetSizeHandler() {
  }

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

  //fontSize 属性で指定した値を受け取るメソッド
  public void setFontSize(int fontSize) {
    this.fontSize = fontSize;
  }

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

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

解説

fontSize属性の値を受け取る、setFontSize()メソッドを実装します。
 public void setFontSize(int fontSize) {
   this.fontSize = fontSize;
 }

doStartTag()メソッドを実装します。FONTタグを出力し、文字のサイズを指定します。文字のサイズはfontSzie属性に記載されているサイズに乱数を掛けた値を指定します。
  public int doStartTag() throws JspException {
    try {
      out.print("<FONT size=\"" + (int) (Math.random() * fontSize) + "\">");
    } catch (Exception e) {
      throw new JspException(e);
    }
    return EVAL_BODY_INCLUDE;
  }

doEndTag()メソッドを実装します。/FONTタグを出力します。
  public int doEndTag() throws JspException {
    try {
      out.println("</FONT>");
    } catch (Exception e) {
      throw new JspException(e);
    }
    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>/SizeLibrary</taglib-uri>
      <taglib-location>/WEB-INF/tlds/size.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>

実行結果

ページをリロードするたびに文字のサイズがランダムで変わります。



著者
iPentecのプログラマー、最近はAIの積極的な活用にも取り組み中。
とっても恥ずかしがり。
最終更新日: 2024-01-06
作成日: 2011-02-14
iPentec all rights reserverd.