You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-dev@jakarta.apache.org by mo...@apache.org on 2001/08/09 23:58:31 UTC
cvs commit: jakarta-taglibs/dbtags/xml dbtags.xml documentation.html
morgand 01/08/09 14:58:31
Modified: dbtags/src/org/apache/taglibs/dbtags/resultset
ResultSetTag.java
dbtags/xml dbtags.xml documentation.html
Log:
added first pass support for RowSets
Revision Changes Path
1.6 +127 -34 jakarta-taglibs/dbtags/src/org/apache/taglibs/dbtags/resultset/ResultSetTag.java
Index: ResultSetTag.java
===================================================================
RCS file: /home/cvs/jakarta-taglibs/dbtags/src/org/apache/taglibs/dbtags/resultset/ResultSetTag.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- ResultSetTag.java 2001/07/02 18:19:32 1.5
+++ ResultSetTag.java 2001/08/09 21:58:30 1.6
@@ -66,16 +66,18 @@
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.JspTagException;
+import javax.servlet.jsp.PageContext;
/**
- * <p>JSP tag resulSet, executes the query and loops through the results
+ * <p>JSP tag resultSet, executes the query and loops through the results
* for the enclosing statement or preparedstatement tag. The body of
* this tag is executed once per row in the resultset. The optional
* "loop" attribute, which default to true, specifies whether to execute
* the tag body once per row "true", or to simply assign the ResultSet
- * to the page attribute specified by "id".</p>
- *
+ * to the page attribute specified by "id". The optional "name" and "scope"
+ * attributes can be used to retrieve a resultset (or rowset) from context.</p>
+ *
* <p>JSP Tag Lib Descriptor
* <pre>
* <name>resultSet</name>
@@ -87,7 +89,8 @@
* this tag is executed once per row in the resultset. The optional
* "loop" attribute, which default to true, specifies whether to execute
* the tag body once per row "true", or to simply assign the ResultSet
- * to the page attribute specified by "id".</info>
+ * to the page attribute specified by "id". The optional "name" and "scope"
+ * attributes can be used to retrieve a resultset (or rowset) from context.<</info>
* <attribute>
* <name>id</name>
* <required>true</required>
@@ -98,21 +101,86 @@
* <required>false</required>
* <rtexprvalue>false</rtexprvalue>
* </attribute>
+ * <attribute>
+ * <name>name</name>
+ * <required>false</required>
+ * <rtexprvalue>true</rtexprvalue>
+ * </attribute>
+ * <attribute>
+ * <name>scope</name>
+ * <required>false</required>
+ * <rtexprvalue>false</rtexprvalue>
+ * </attribute>
* </pre>
- *
+ *
* @author Morgan Delagrange
+ * @author Ted Husted
+ * @author Craig McClanahan
* @see org.apache.taglibs.dbtags.statement.StatementImplTag
* @see org.apache.taglibs.dbtags.statement.QueryTag
* @see org.apache.taglibs.dbtags.preparedstatement.PreparedStatementImplTag
*/
public class ResultSetTag extends BodyTagSupport{
-
+
private Statement _statement = null;
private ResultSet _rset = null;
private boolean _shouldLoop = true;
+ private String _name = null;
+ private String _scope = null;
private int _rowCount = 0;
private StatementTag _stmtTag = null;
+
+ /**
+ * Locate and return the specified bean, from an optionally specified
+ * scope, in the specified page context. If no such bean is found,
+ * return <code>null</code> instead. If an exception is thrown, it will
+ * have already been saved via a call to <code>saveException()</code>.
+ *
+ * @param pageContext Page context to be searched
+ * @param name Name of the bean to be retrieved
+ * @param scope Scope to be searched (page, request, session, application)
+ * or <code>null</code> to use <code>findAttribute()</code> instead
+ *
+ * @exception JspException if an invalid scope name
+ * is requested
+ */
+ protected Object lookup(PageContext pageContext,
+ String name, String scope) throws JspTagException {
+
+ Object bean = null;
+ if (scope == null)
+ bean = pageContext.findAttribute(name);
+ else if (scope.equalsIgnoreCase("page"))
+ bean = pageContext.getAttribute(name, PageContext.PAGE_SCOPE);
+ else if (scope.equalsIgnoreCase("request"))
+ bean = pageContext.getAttribute(name, PageContext.REQUEST_SCOPE);
+ else if (scope.equalsIgnoreCase("session"))
+ bean = pageContext.getAttribute(name, PageContext.SESSION_SCOPE);
+ else if (scope.equalsIgnoreCase("application"))
+ bean =
+ pageContext.getAttribute(name, PageContext.APPLICATION_SCOPE);
+ else {
+ JspTagException e = new JspTagException("Invalid scope " + scope);
+ throw e;
+ }
+ return (bean);
+
+ }
+
+ /**
+ * Name of the bean that contains the rowset to process.
+ */
+
+
+ public void setName(String name) {
+ _name = name;
+ }
+
+ public void setScope(String scope) {
+ _scope = scope;
+ }
+
public void setLoop(boolean shouldLoop) {
_shouldLoop = shouldLoop;
}
@@ -122,53 +190,72 @@
}
public int doStartTag() throws JspTagException {
- try {
- _stmtTag =
- (StatementTag) findAncestorWithClass(this, Class.forName("org.apache.taglibs.dbtags.statement.StatementTag"));
- _rset = _stmtTag.executeQuery();
- pageContext.setAttribute(getId(), _rset);
- if (_shouldLoop == false) {
- return EVAL_BODY_TAG;
- }
+ try {
+ // if name property given, retrieve resultset from context
+ // may also be a related class, like RowSet
+ if (_name!=null) {
+ _rset = (ResultSet) lookup(pageContext, _name, _scope);
+ }
+ else {
+ _stmtTag =
+ (StatementTag) findAncestorWithClass(this,
+ Class.forName("org.apache.taglibs.dbtags.statement.StatementTag"));
+ _rset = _stmtTag.executeQuery();
+ }
+
+ pageContext.setAttribute(getId(), _rset);
+
+ if (_shouldLoop == false) {
+ return EVAL_BODY_TAG;
+ }
+
+ if (_rset.next() == false) {
+ if (_stmtTag!=null) {
+ _stmtTag.setTotalRowCount(_rowCount);
+ }
+ return SKIP_BODY;
+ }
- if (_rset.next() == false) {
- _stmtTag.setTotalRowCount(_rowCount);
- return SKIP_BODY;
+ ++_rowCount;
+ if (_stmtTag!=null) {
+ _stmtTag.setTotalRowCount(_rowCount);
}
- ++_rowCount;
- _stmtTag.setTotalRowCount(_rowCount);
-
} catch (ClassNotFoundException e) {
throw new JspTagException(e.toString());
} catch (SQLException e) {
throw new JspTagException(e.toString());
}
-
+
return EVAL_BODY_TAG;
}
-
+
public int doEndTag() throws JspTagException{
pageContext.removeAttribute(getId());
- try {
+ try {
if (getBodyContent() != null && getPreviousOut() != null) {
getPreviousOut().write(getBodyContent().getString());
- }
+ }
} catch (IOException e) {
throw new JspTagException(e.toString());
} finally {
- try {
- _rset.close();
- }
+ try {
+ _rset.close();
+ }
catch (SQLException e) {
// it's not fatal if the result set cannot be closed
- e.printStackTrace();
+ e.printStackTrace();
}
}
+
+ // we have to call this guy manually now
+ // with the spec clarification
+ release();
+
return EVAL_PAGE;
}
-
+
public int doAfterBody() throws JspTagException{
if (_shouldLoop == false) {
@@ -178,23 +265,29 @@
try {
if (_rset.next() == true) {
++_rowCount;
- _stmtTag.setTotalRowCount(_rowCount);
- return EVAL_BODY_TAG;
+ if (_stmtTag!=null) {
+ _stmtTag.setTotalRowCount(_rowCount);
+ }
+ return EVAL_BODY_TAG;
}
} catch (SQLException e) {
throw new JspTagException(e.toString());
}
- _stmtTag.setTotalRowCount(_rowCount);
+ if (_stmtTag!=null) {
+ _stmtTag.setTotalRowCount(_rowCount);
+ }
return EVAL_PAGE;
}
-
+
public void release() {
_statement = null;
_rset = null;
_shouldLoop = true;
+ _name = null;
+ _scope = null;
_rowCount = 0;
_stmtTag = null;
}
-
+
}
1.4 +40 -2 jakarta-taglibs/dbtags/xml/dbtags.xml
Index: dbtags.xml
===================================================================
RCS file: /home/cvs/jakarta-taglibs/dbtags/xml/dbtags.xml,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- dbtags.xml 2001/08/09 18:45:34 1.3
+++ dbtags.xml 2001/08/09 21:58:30 1.4
@@ -962,8 +962,9 @@
</description>
<availability>1.0</availability>
- <restrictions>Use within a statement or preparedStatement
- and after a query.</restrictions>
+ <restrictions>If a name attribute is not supplied, use within a statement or
+ preparedStatement and after a query. If a name attribute is supplied,
+ there are no restrictions.</restrictions>
<variable>
<name-from-attribute>id</name-from-attribute>
@@ -1005,6 +1006,31 @@
<availability>1.0</availability>
</attribute>
+ <attribute>
+ <name>name</name>
+ <required>no</required>
+ <rtexprvalue>no</rtexprvalue>
+ <description>
+ Name of an attribute containing a ResultSet object. If you pull a
+ ResultSet object from an attribute, it is not necessary to
+ place this tag inside of a statement.
+ </description>
+ <availability>1.0</availability>
+ </attribute>
+
+ <attribute>
+ <name>scope</name>
+ <required>no</required>
+ <rtexprvalue>no</rtexprvalue>
+ <description>
+ Scope (page, request, session, or application)
+ to search for the ResultSet attribute indicated in the "name"
+ attribute. If this is not supplied, we use the default findAttribute()
+ behaviour.
+ </description>
+ <availability>1.0</availability>
+ </attribute>
+
<example>
<usage>
<code>
@@ -1581,10 +1607,22 @@
</taglib>
+<revision release="Pre Beta" date="05/01/2001">
+ <description>
+ Changed Tag Library Name from JDBC to DBTags.
+ </description>
+</revision>
+
<revision release="Pre Beta" date="08/06/2001">
<description>
Clean up of tag library prior to performing a beta
release, moving toward an official release.
+ </description>
+</revision>
+
+<revision release="Pre Beta" date="08/09/2001">
+ <description>
+ Added name attribute to resultset tag for use with rowsets.
</description>
</revision>
1.2 +23 -0 jakarta-taglibs/dbtags/xml/documentation.html
Index: documentation.html
===================================================================
RCS file: /home/cvs/jakarta-taglibs/dbtags/xml/documentation.html,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- documentation.html 2001/08/06 22:06:02 1.1
+++ documentation.html 2001/08/09 21:58:30 1.2
@@ -341,4 +341,27 @@
</sql:statement></pre>
+<p><b>using RowSets</b></p>
+
+<p>You may also use the resultSet tag with a RowSet object. By setting the option
+ "name", the resultSet tag will look for a ResultSet object (including RowSets)
+stored under that name in the page, request, or session contexts. By setting the
+option "scope", you can specify which context contains your ResultSet/RowSet.
+Note that when you read a ResultSet/RowSet from an attribute, the resultSet tag
+does <i>not</i> need to be inside of a statement tag.</p>
+
+<pre>
+ <%-- loop through the rows of your ResultSet/RowSet,
+ wherever it came from --%>
+ <sql:resultSet id="rset1" name="rsetAtt">
+ <tr>
+ <td><sql:getColumn position="1"/></td>
+ <td><sql:getColumn position="2"/></td>
+ <td><sql:getColumn position="3"/>
+ <%-- print out a comment if the book has no description --%>
+ <sql:wasNull>[no description]</sql:wasNull></td>
+ </tr>
+ </sql:resultSet></pre>
+
</span>
+