カスタムタグ処理でどのようにメソッドが呼び出されるか確認します。
コード (JSPファイル:bodytagsupport.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="/AppendixCustomLibrary" prefix="appendix" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Appendix Custom Library</title>
</head>
<body>
<h1>Appendix Custom Library</h1>
<BR><HR><BR>
<FONT SIZE="20">
<appendix:AppendixCustom>
Test Message
</appendix:AppendixCustom>
</FONT>
</body>
</html>
コード (AppendixCustom.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>Appendix Tag Library</short-name>
<uri>/WEB-INF/tlds/AppendixCustom</uri>
<tag>
<name>AppendixCustom</name>
<tag-class>webApplication32.MyBodyTag</tag-class>
</tag>
</taglib>
コード (タグ処理クラス:MyBodyTag.java)
package webApplication32;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;
public class MyBodyTag extends BodyTagSupport {
private PageContext pc = null;
private BodyContent bc = null;
private JspWriter out = null;
public void setPageContext(PageContext pc) {
this.pc = pc;
this.out = pc.getOut();
//標準出力に出力
System.out.println("MyBodyTag : setPageContext()");
}
public int doStartTag() throws JspException {
try {
out.println("doStartTag() <BR>");
} catch (Exception e) {
throw new JspException(e);
}
//標準出力に出力
System.out.println("MyBodyTag : doStartTag()");
//BODY 処理を行う
return EVAL_BODY_BUFFERED;
}
public void setBodyContent(BodyContent bc) {
this.bc = bc;
//標準出力に出力
System.out.println("MyBodyTag : setBodyContent()");
}
public void doInitBody() throws JspException {
try {
out.println("doInitBody() <BR>");
} catch (Exception e) {
throw new JspException(e);
}
}
public int doAfterBody() throws JspException {
String message = "";
String tmp = null;
try {
//BODY情報から文字列を読み込むストリームを取得
BufferedReader br = new BufferedReader(bc.getReader());
//全行の文字列を読み込み連結
while ((tmp = br.readLine()) != null) {
message = message + tmp;
}
out.println("BodyMessage : " + message + "<BR>");
out.println("doAfterBody() <BR>");
} catch (Exception e) {
throw new JspException(e);
}
//標準出力に出力
System.out.println("MyBodyTag : doAfterBody()");
//BODY処理をスキップ
return SKIP_BODY;
}
public int doEndTag() throws JspException {
try {
out.println("doEndTag() <BR>");
} catch (Exception e) {
throw new JspException(e);
}
//標準出力に出力
System.out.println("MyBodyTag : doEndTag()");
//JSPページの読み込みを続ける
return EVAL_PAGE;
}
/** Creates a new instance of MyBodyTag */
public MyBodyTag() {
}
}
コード (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>/AppendixCustomLibrary</taglib-uri>
<taglib-location>/WEB-INF/tlds/AppendixCustom.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