Java Reference
In-Depth Information
StringBuilder result =
new
StringBuilder();
for
(Contact contact : contacts) {
result.append(contact).append(',');
}
result.setLength(result.length() - 1);
String recpt = (previous ==
null
) ? "" : previous + ",";
return
recpt + result.toString();
}
return
previous;
}
This code looks fine, but it won't work! Well, not yet, anyway, and I'll
tell you why. To decide how to populate a form input field, Stripes uses
a population strategy, which is an extension represented by the
Popula-
tionStrategy
interface. Out of the box, Stripes uses the
DefaultPopulation-
Strategy
, which prefers values from request parameters to values from
action bean properties when populating a form input field. Consider
this scenario, in which the user does the following:
• Types
Fred
in the To field
• Selects
Daniel Greene
from the contact box
• Clicks the arrow button next to the To field
The request parameters will be as follows:
message.to=Fred
contacts=(ID of Daniel Greene)
In the
addTo
( ) event handler, our clever code combines the two param-
eters to set the
message.to
action bean property to
Fred, Daniel Greene
.
But the
message.to=
request parameter is still “Fred,” and that's the
value used by Stripes to populate the To text field.
So, what's a developer to do? Stripes provides an alternate population
strategy,
BeanFirstPopulationStrategy
, which looks at the action bean prop-
erty before the request parameter. That's what we want. Since it is a
Stripes extension, we just have to create a class that extends
BeanFirst-
PopulationStrategy
and add it to our extension package,
stripesbook.ext
:
package
stripesbook.ext;
public class
MyPopulationStrategy
extends
BeanFirstPopulationStrategy{
}
Search WWH ::
Custom Search