解答例 - 実習課題2 - 7.繰り返し機能を持つカスタムタグ
(実習課題2)
7.1のサンプルカスタムタグ「ListTag」を以下の点で改良し、それを使用したJSPページを作成しなさい。
- 「list」属性で指定されたオブジェクトが「java.util.List」ではなく、「java.util.Map」であった場合、その値を順に「pageContext」に登録するようにする事。
解答例
/* * ListTag.java TECHSCORE Java JSP7実習課題2 * Copyright (c) 2004 Four-Dimensional Data, Inc. */ package com.techscore.jsp.chapter7.exercise2; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.IterationTag; import javax.servlet.jsp.tagext.Tag; public class ListTag implements IterationTag { private PageContext pageContext = null; private Tag parent = null; private Map map = null; private Iterator iterator = null; private String item = ""; public void setList(String name) { Object object = pageContext.findAttribute(name); if (object instanceof List) { List list = (List) object; this.iterator = list.iterator(); } else if (object instanceof Map) { map = (Map) object; this.iterator = map.values().iterator(); } } public void setPageContext(PageContext pageContext) { this.pageContext = pageContext; } public int doStartTag() throws JspException { if (iterator.hasNext()) { pageContext.setAttribute(item, iterator.next()); return (EVAL_BODY_INCLUDE); } else { return (SKIP_BODY); } } public int doAfterBody() throws JspException { if (iterator.hasNext()) { pageContext.setAttribute(item, iterator.next()); return (EVAL_BODY_AGAIN); } else { return (SKIP_BODY); } } public int doEndTag() throws JspException { return (EVAL_PAGE); } public void release() { } public void setItem(String item) { this.item = item; } public Tag getParent() { return parent; } public void setParent(Tag parent) { this.parent = parent; } }
<!-- taglib.jsp --> <!-- TECHSCORE Java JSP 7章 実習課題2 --> <!-- Copyright (c) 2004 Four-Dimensional Data, Inc. --> <%@page contentType="text/html; charset=Windows-31J" import="java.util.*" %> <%@taglib uri="http://www.techscore.com/tags/myTag" prefix="myTag" %> <html> <head><title>JSP7章 実習課題2</title></head> <body> <h3>JSP7章 実習課題2</h3> <% Map map = new HashMap(); map.put("key1", "test1"); map.put("key2", "test2"); map.put("key3", "test3"); application.setAttribute("list", map); %> <table border="1"><tr> <myTag:listTag7_2 list="list" item="item"> <td><%= pageContext.getAttribute("item") %></td> </myTag:listTag7_2> </tr></table> </body> </html>
<?xml version="1.0" ?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>example tags</short-name> <tag> <name>listTag7_2</name> <tag-class>com.techscore.jsp.chapter7.exercise2.ListTag</tag-class> <body-content>JSP</body-content> <attribute> <name>list</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>item</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>