On 1/3/2001 at 11:06 AM Craig R. McClanahan wrote:
>If you do it this way, do you need more than one QueryBean? Couldn't
you create a generic one that processes different kinds of query
strings based on the name of the resource you told it to read?
Now that you mention, it could be used a generic bean now; I've been
using the name property as the key into the resource, which is meant to
store all the queries used anywhere by the application. So that part is
probably working already.
I was subclassing it to provide a way to hand Struts a collection
(based on the ResultSet) for the tags to play with. When we upgrade the
tags for rowsets, I might be able to skip this step and use the rowset
where I'm generate a collection now. Today, I'm just using the subclass
to do something like
public FruitSalesDetail[] fetchDetail(int from, int count) throws
SQLException {
// TODO - Generate exception if from < 1
setFrom(from);
// optimistic result
setCountResult(count);
// calculate where Prev would have been; but don't go negative
if (from>count) {setPrev(from-count);} else {setPrev(1);}
FruitSalesDetail[] detail = new FruitSalesDetail[count];
// resultSet.absolute(from); // FIXME if 1 starts at 2 ?
for (int i = 1; i < from; i++) {
result.next();
}
for (int i = 0; i < count; i++) {
if (result.next()) {
detail[i] = new FruitSalesDetail();
detail[i].setYear(result.getInt(1));
detail[i].setQuarter(result.getInt(2));
detail[i].setApples(result.getInt(3));
detail[i].setApplesales (result.getFloat(4));
detail[i].setOranges(result.getInt(5));
detail[i].setOrangesales (result.getFloat(6));
detail[i].setTopseller(result.getString(7));
from++;
}
else {
setCountResult(i);
break;
}
}
if (result.next()) {setNext(from);} else {setNext(1);}
return(detail);
}
The "detail" it returns gets inserted directly into the page context
page.setAttribute("fruitSalesDetail",salesQuery.fetchDetail());
where iterate can snag it with
<logic:iterate id="row" name="fruitSalesDetail" length="length"
scope="page">
The next/prev stuff lets me call the JSP again for the next block of
records, or rollback with
if (action.equals("prev")) { sQuery.setNext(sQuery.getPrev()); }
// where sQuery is a QueryBean property of my ActionForm.
The 30,000 foot view is
ActionForm -> ActionDataForm -> FruitSalesForm
// Provides JDBC and PageContext hooks
ActionFormBean -> QueryFormBean -> FruitSalesQuery
// Implements JDBC calls; Maintains query state between requests.
Action -> FruitSalesAction
// Resets FruitSalesQuery bean to start new round of requests.
FruitSalesDetail
// Generic bean to encapsulate fields returned by query
// Inserted as an array into page context by FruitSalesForm
Of course, suggestions for an alternate model would be appreciated! The
endgame is just to provide something the iterate and option tags can
use.
A working example is available at http://husted.com/about/struts
-- Ted Husted, Husted dot Com, Fairport NY USA.
-- Custom Software ~ Technical Services.
-- Tel 716 425-0252; Fax 716 223-2506.
-- http://www.husted.com/