解答例 - 実習課題2 - 10.タグの検証/スクリプト変数の定義
(実習課題2)
実習課題1のカスタムタグを改良しなさい。
- id属性で指定した名前のスクリプト変数を定義する事。スクリプト変数のスコープはNESTEDで良い。
解答例
/* * RequestTag.java TECHSCORE Java JSP10実習課題2 * Copyright (c) 2004 Four-Dimensional Data, Inc. */ package com.techscore.jsp.chapter10.exercise2; 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; } }
/* * RequestTagExtraInfo.java TECHSCORE Java JSP10実習課題2 * Copyright (c) 2004 Four-Dimensional Data, Inc. */ package com.techscore.jsp.chapter10.exercise2; import javax.servlet.jsp.tagext.TagData; import javax.servlet.jsp.tagext.TagExtraInfo; import javax.servlet.jsp.tagext.VariableInfo; public class RequestTagExtraInfo extends TagExtraInfo { private boolean textCheck(String text) { if (text.indexOf(",") == -1) { return false; } return true; } public VariableInfo[] getVariableInfo(TagData tagData) { VariableInfo[] variableInfos = new VariableInfo[1]; variableInfos[0] = new VariableInfo(tagData.getAttributeString("id"), "java.lang.String[]", true, VariableInfo.NESTED); return (variableInfos); } public boolean isValid(TagData tagData) { if (TagData.REQUEST_TIME_VALUE == tagData.getAttribute("text") || TagData.REQUEST_TIME_VALUE == tagData.getAttribute("request")) { return false; } String text = tagData.getAttributeString("text"); String request = tagData.getAttributeString("request"); if (text != null && !"".equals(text)) { if (request != null && !"".equals(request)) { return false; } else if (!textCheck(text)) { return false; } } else { if (request == null || "".equals(request)) { return false; } } return true; } }
<!-- taglib.jsp --> <!-- TECHSCORE Java JSP 10章 実習課題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>JSP10章 実習課題2</title></head> <body> <h3>JSP10章 実習課題2</h3> <% String[] strings = null; %> <b>text属性の場合</b><br> <myTag:request10_2 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_2> <br> <b>request属性の場合</b><br> <myTag:request10_2 id="set2" scope="session" request="array"> <% strings = (String[])session.getAttribute("set2"); for(int i = 0; i < strings.length; i++){ %><%= strings[i] %><br><% } %> </myTag:request10_2> <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_2</name> <tag-class>com.techscore.jsp.chapter10.exercise2.RequestTag</tag-class> <tei-class>com.techscore.jsp.chapter10.exercise2.RequestTagExtraInfo</tei-class> <body-content>JSP</body-content> <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>