解答例 - 実習課題1 - 9.親子タグ
(実習課題1)
サンプルのカスタムタグを改良し、それを利用したJSPページを作成しなさい。
- 「Update」タグのボディ部に「java.sql.PreparedStatement」で許しているような「?」付きのSQL文を記載できるようにする。
- 「?」の値は、「Update」タグと親子関係にある「Parameter」タグで指定する。その値は「Parameter」タグの属性「value」で指定する。
- 「?」に指定できる値のタイプは文字列のみと限定してよい。
- JSPページでの利用は以下のような形式となります。
<example:Update>update product set type=? where p_num=111 <example:Parameter value="sedan" /> </example:Update>
- (ヒント)TLDで「Update」タグの設定を変更しなければならない箇所がある。
解答例
/* * ParameterTag.java TECHSCORE Java JSP9実習課題1 * Copyright (c) 2004 Four-Dimensional Data, Inc. */ package com.techscore.jsp.chapter9.exercise1; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.TagSupport; public class ParameterTag extends TagSupport { private String value = null; public void setValue(String value) { this.value = value; } public int doStartTag() throws JspException { UpdateTag tag = (UpdateTag)findAncestorWithClass(this, UpdateTag.class); tag.getParamList().add(value); return EVAL_PAGE; } }
/* * TransactionTag.java TECHSCORE Java JSP9実習課題1 * Copyright (c) 2004 Four-Dimensional Data, Inc. */ package com.techscore.jsp.chapter9.exercise1; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.TagSupport; public class TransactionTag extends TagSupport { private String jdbcURL; private String user; private String password; public void setJdbcURL(String jdbcURL) { this.jdbcURL = jdbcURL; } public void setPassword(String password) { this.password = password; } public void setUser(String user) { this.user = user; } public int doStartTag() throws JspException { try { Class.forName("org.postgresql.Driver"); } catch (ClassNotFoundException e) { throw new JspException(e.getMessage()); } try { Connection conn = DriverManager.getConnection(jdbcURL, user, password); conn.setAutoCommit(false); setValue("connection", conn); } catch (SQLException e) { throw new JspException(e.getMessage()); } return (EVAL_BODY_INCLUDE); } public int doEndTag() throws JspException { Connection conn = (Connection) getValue("connection"); try { conn.commit(); conn.close(); } catch (SQLException e) { throw new JspException(e.getMessage()); } return (EVAL_PAGE); } }
/* * UpdateTag.java TECHSCORE Java JSP9実習課題1 * Copyright (c) 2004 Four-Dimensional Data, Inc. */ package com.techscore.jsp.chapter9.exercise1; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyTagSupport; public class UpdateTag extends BodyTagSupport { private List paramList = new LinkedList(); public List getParamList() { return paramList; } public int doAfterBody() throws JspException { TransactionTag transactionTag = (TransactionTag) findAncestorWithClass(this, TransactionTag.class); Connection conn = (Connection) transactionTag.getValue("connection"); String sql = bodyContent.getString(); pageContext.getServletContext().log(sql); PreparedStatement statement = null; ResultSet result = null; try { statement = conn.prepareStatement(sql); int count = 1; Iterator iterator = paramList.iterator(); while (iterator.hasNext()) { statement.setString(count++, (String) iterator.next()); } statement.executeUpdate(); } catch (SQLException e) { throw new JspException(e.getMessage()); } finally { try { if (result != null) { result.close(); } if (statement != null) { statement.close(); } } catch (SQLException e) {} } return SKIP_BODY; } }
<!-- taglib.jsp --> <!-- TECHSCORE Java JSP 9章 実習課題1 --> <!-- 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="example" %> <html> <head><title>JSP9章 実習課題1</title></head> <body> <h3>JSP9章 実習課題1</h3> <example:Transaction jdbcURL="jdbc:postgresql:Training" user="postgres" password=""> <example:Update>update product set type=? where p_num=250 <example:Parameter value="Vits" /> </example:Update> </example:Transaction> </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>Transaction</name> <tag-class>com.techscore.jsp.chapter9.exercise1.TransactionTag</tag-class> <body-content>JSP</body-content> <attribute> <name>jdbcURL</name> <required>true</required> </attribute> <attribute> <name>user</name> <required>true</required> </attribute> <attribute> <name>password</name> <required>true</required> </attribute> </tag> <tag> <name>Update</name> <tag-class>com.techscore.jsp.chapter9.exercise1.UpdateTag</tag-class> <body-content>JSP</body-content> </tag> <tag> <name>Parameter</name> <tag-class>com.techscore.jsp.chapter9.exercise1.ParameterTag</tag-class> <body-content>tagdependent</body-content> <attribute> <name>value</name> <required>true</required> </attribute> </tag> </taglib>