20 CHAPTER 30 Extending ColdFusion with CFX
Listing 30.3 (continued)
// Fill the array with this ResultSet’s column names
for (int col = 1; col <= rsmd.getColumnCount(); col++ ) {
arColumns[col-1] = rsmd.getColumnName(col);
};
// Create a new com.allaire.Query object
Query q = response.addQuery(name, arColumns);
// For each row of the ResultSet...
while ( rs.next() ) {
// Add a row to the CFML query
int row = q.addRow();
// For each column of the ResultSet...
for (int col = 1; col <= rsmd.getColumnCount(); col++) {
// Copy the data at this row/column from ResultSet to Query
// (an error will be thrown if the data can’t be read as a string)
q.setData(row, col, rs.getString(col));
};
};
}
// Helper function that returns the value of an attribute
// An exception is thrown if the attribute was not provided
private String getTagAttr(String name, Request request)
throws Exception
{
if ( request.attributeExists(name) ) {
return request.getAttribute(name);
} else {
throw new Exception(msgError + “The “+name+” attribute is required.”);
};
}
// Helper function that returns the value of an attribute
// If the attribute is not provided, it defaults to def.
private String getTagAttr(String name, Request request, String def)
throws Exception
{
return request.attributeExists(name) ? request.getAttribute(name) : def;
}
}
Just by scanning over this example listing with your eye, you can divide this tag into three concep-
tual parts:
■
The processRequest() handler method, which is executed by ColdFusion whenever
the CFX tag is used in a ColdFusion page. This portion of the code does the work
of connecting to the database and retrieving the requested information as a
java.sql.ResultSet object.
Komentarze do niniejszej Instrukcji