コード (attcustom.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="/AttCustomLibrary" prefix="title" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Attribute Custom Library</title>
</head>
<body>
<%-- 属性値にあわせた文字サイズとテーブル枠の太さで表示する --%>
<title:AttCustom fontSize="20" tableBorderSize="20">
Attrubute Custom Library
</title:AttCustom>
<title:AttCustom fontSize="4" tableBorderSize="4">
テスト
</title:AttCustom>
<BR><HR><BR>
<P>This page is Atribute Custom Library</P>
</body>
</html>
コード (AttCustom.java)
package webApplication22;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
public class AttCustom extends TagSupport {
private PageContext pc = null;
private JspWriter out = null;
//文字のサイズの大きさを格納する変数
//初期サイズは20pt
private int fontSize = 20;
//テーブル枠の太さを格納する変数
//初期設定は0
private int tableBorderSize = 0;
//fontSize 属性で指定した値を受け取るメソッド
public void setFontSize(int fontSize) {
this.fontSize = fontSize;
}
//tableBorderSize 属性で指定した値を受け取るメソッド
public void setTableBorderSize(int tableBorderSize) {
this.tableBorderSize = tableBorderSize;
}
public void setPageContext(PageContext pc) {
this.pc = pc;
this.out = pc.getOut();
}
public int doStartTag() throws JspException {
try {
//中央揃えテーブル枠としてのテーブルタグと文字サイズ 20pを指定
out.println("<TABLE align=\"center\" border=\""
+ tableBorderSize + "\">");
out.print("<TR><TD><FONT size=\"" + fontSize + "\">");
} 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;
}
/** Creates a new instance of AttCustom */
public AttCustom() {
}
}
解説
public void setFontSize(int fontSize) {
this.fontSize = fontSize;
}
fontSize属性の値を受け取るsetFontSize()メソッドを実装します。値はメンバ変数に格納します。
public void setTableBorderSize(int tableBorderSize) {
this.tableBorderSize = tableBorderSize;
}
tableBorderSize属性の値を受け取るsetTableBorderSize()メソッドを実装します。値はメンバ変数にっ格納します。
public void setPageContext(PageContext pc) {
this.pc = pc;
this.out = pc.getOut();
}
setPageContext()メソッドを実装します。与えられたPageContextとJspWriterをメンバ変数に格納します。
public int doStartTag() throws JspException {
doStartTag()メソッドを実装します。開始タグで処理するアルゴリズムを実装します。今回の例ではテーブルを開始するテーブルタグを出力します。また、フォントサイズやボーダーサイズはメンバ変数に格納されている値を用います。
public int doEndTag() throws JspException {
doEndTag()メソッドを実装します。終了タグで処理するアルゴリズムを実装します。今回の例ではテーブルを終了するテーブルタグを出力します。
コード (attcustom.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.2</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>Attribute Tag Library</short-name>
<uri>/WEB-INF/tlds/attcustom</uri>
<tag>
<name>AttCustom</name>
<tag-class>webApplication22.AttCustom</tag-class>
<attribute>
<!--フォントサイズを設定する属性-->
<!--属性を必ず指定しなくてもよい-->
<name>fontSize</name>
<required>false</required>
</attribute>
<attribute>
<!--テーブルの枠の太さを設定する属性-->
<!--属性を必ず指定しなくてはならない-->
<name>tableBorderSize</name>
<required>true</required>
</attribute>
</tag>
</taglib>
コード (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>/AttCustomLibrary</taglib-uri>
<taglib-location>/WEB-INF/tlds/attcustom.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>Attribute Custom Library</title>
</head>
<body>
<TABLE align="center" border="20">
<TR><TD><FONT size="20">
Attrubute Custom Library
<FONT></TD></TR>
</TABLE>
<TABLE align="center" border="4">
<TR><TD><FONT size="4">
テスト
<FONT></TD></TR>
</TABLE>
<BR><HR><BR>
<P>This page is Atribute Custom Library</P>
</body>
</html>
著者
iPentecのプログラマー、最近はAIの積極的な活用にも取り組み中。
とっても恥ずかしがり。
最終更新日: 2024-01-06
作成日: 2011-02-13