目次へ

解答例 - 実習課題4 - 6.カスタムタグの基本2

(実習課題4)

4節のサンプルタグ(AttributeExistタグ)を改良しなさい。

  • オブジェクトが見つかった場合、どこのスコープから見つかったのか表示するようにする事。

解答例

/*
 * JspChapter6_4Tag.java TECHSCORE Java JSP6実習課題4
 * Copyright (c) 2004 Four-Dimensional Data, Inc.
 */
package com.techscore.jsp.chapter6.exercise4;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

public class JspChapter6_4Tag implements Tag {

    private PageContext pageContext = null;

    private Tag parentTag = null;
    private String name = "";

    public void setName(String name) {
        this.name = name;
    }

    public void setPageContext(PageContext pageContext) {
        this.pageContext = pageContext;
    }

    public void setParent(Tag parentTag) {
        this.parentTag = parentTag;
    }

    public Tag getParent() {
        return parentTag;
    }

    public int doStartTag() throws JspException {
        return SKIP_BODY;
    }

    public int doEndTag() throws JspException {
        JspWriter writer = pageContext.getOut();
        try {
            if (pageContext.findAttribute(name) == null) {
                writer.println(name + " attribute not found.");
            } else {
                String scope = null;
                if (pageContext.getAttribute(name,
                                             PageContext.APPLICATION_SCOPE) != null) {
                    scope = "application";
                } else if (pageContext.getAttribute(name,
                                                    PageContext.SESSION_SCOPE) != null) {
                    scope = "session";
                } else if (pageContext.getAttribute(name,
                                                    PageContext.PAGE_SCOPE) != null) {
                    scope = "page";
                } else if (pageContext.getAttribute(name,
                                                    PageContext.REQUEST_SCOPE) != null) {
                    scope = "request";
                }

                writer.println(name + " attribute found by " + scope
                        + " scope.");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return EVAL_PAGE;
    }

    public void release() {}
}
<!-- taglib.jsp -->
<!-- TECHSCORE Java JSP 6章 実習課題4 -->
<!-- Copyright (c) 2004 Four-Dimensional Data, Inc. -->

<%@page contentType="text/html; charset=Windows-31J" %>
<%@taglib uri="http://www.techscore.com/tags/myTag" prefix="myTag" %>
<html>
  <head><title>JSP6章 実習課題4</title></head>
  <body>
    <h3>JSP6章 実習課題4</h3>
    <myTag:findAttribute6_4 name="count"/>
  </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>findAttribute6_4</name>
    <tag-class>com.techscore.jsp.chapter6.exercise4.JspChapter6_4Tag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
      <name>name</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

</taglib>

↑このページの先頭へ

こちらもチェック!

PR
  • XMLDB.jp