解答例 - 実習課題3 - 10.タグの検証/スクリプト変数の定義
(実習課題3)
実習課題2で作成したカスタムタグのTLDファイルにスクリプト変数の情報を追加しなさい。
解答例
/*
* RequestTag.java TECHSCORE Java JSP10実習課題3
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*/
package com.techscore.jsp.chapter10.exercise3;
import java.util.ArrayList;
import java.util.StringTokenizer;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
public class RequestTag extends TagSupport {
private String request = null;
private String text = null;
private String scope = null;
public int doStartTag() throws JspException {
//pageContext.setAttribute(this.id, "set1", PageContext.PAGE_SCOPE);
String[] strings = null;
if (request != null) {
strings = pageContext.getRequest().getParameterValues(request);
} else if (text != null) {
strings = divideString(text);
}
if ("session".equals(scope)) {
pageContext.setAttribute(id, strings, PageContext.SESSION_SCOPE);
} else if ("request".equals(scope)) {
pageContext.setAttribute(id, strings, PageContext.REQUEST_SCOPE);
} else if ("application".equals(scope)) {
pageContext.setAttribute(id, strings, PageContext.APPLICATION_SCOPE);
} else if ("page".equals(scope)) {
pageContext.setAttribute(id, strings, PageContext.PAGE_SCOPE);
}
return EVAL_PAGE;
}
private String[] divideString(String text) {
StringTokenizer tokenizer = new StringTokenizer(text, ",");
ArrayList arrayList = new ArrayList();
while (tokenizer.hasMoreTokens()) {
arrayList.add(tokenizer.nextToken());
}
String[] strings = new String[0];
strings = (String[]) arrayList.toArray(strings);
return strings;
}
private String[] objectsToStrings(Object[] objects) {
int len = objects.length;
String[] strings = new String[len];
for (int i = 0; i < len; i++) {
strings[i] = objects[i].toString();
}
return strings;
}
public void setRequest(String request) {
this.request = request;
}
public void setText(String text) {
this.text = text;
}
public void setScope(String scope) {
this.scope = scope;
}
}
<!-- taglib.jsp -->
<!-- TECHSCORE Java JSP 10章 実習課題3 -->
<!-- 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>JSP10章 実習課題3</title></head>
<body>
<h3>JSP10章 実習課題3</h3>
<% String[] strings = null; %>
<b>text属性の場合</b><br>
<myTag:request10_3 id="set1" scope="request" text="0,1,2,3,4,5,6,7,8,9">
<%
strings = (String[])request.getAttribute("set1");
for(int i = 0; i < strings.length; i++){
%><%= strings[i] %><br><%
}
%>
</myTag:request10_3>
<br>
<b>request属性の場合</b><br>
<myTag:request10_3 id="set2" scope="session" request="array">
<%
strings = (String[])session.getAttribute("set2");
for(int i = 0; i < strings.length; i++){
%><%= strings[i] %><br><%
}
%>
</myTag:request10_3>
<br>
</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>request10_3</name>
<tag-class>com.techscore.jsp.chapter10.exercise3.RequestTag</tag-class>
<body-content>JSP</body-content>
<variable>
<name-from-attribute>id</name-from-attribute>
<variable-class>java.lang.String[]</variable-class>
<declare>true</declare>
<scope>NESTED</scope>
</variable>
<attribute>
<name>id</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>scope</name>
<required>true</required>
</attribute>
<attribute>
<name>request</name>
<required>false</required>
</attribute>
<attribute>
<name>text</name>
<required>false</required>
</attribute>
</tag>
</taglib>

