In my first post, I had a couple of issues, and I don't want to lose sight
of the other, because it is actually a bigger problem. That is in the
case that I do just use a simple list of strings, like this:
new DropDownChoice("id", LIST_OF_STRINGS);
if you do this then the model of the dropdown is lookup though the parent structure
(looked for a compound model) and then the "id" is used to get a property of the value
that is in the object thats in the compound model.
I don't get any default values (either "Choose One" or from my properties
The id was not null!
Only when the selection is null and null is allowed Choose One is added.
file), instead the default selected value is the first in the list. If I
do this:
new DropDownChoice("id", new Model(), LIST_OF_STRINGS)
now the selected is null (because you use a model with nothing)
So choose one is generated
I get the a default value, but it will always be marked as selected in the
html, in addition to whatever is actually selected, so what is actually
selected is never persisted to the object model.
What do you mean to what object model?
There you just have a model. And that model will hold the selection when you select something
Only it won't go automatic to youre data object because you just use a default empty model.
I do remeber seeing something about the selected object being of the same
type, and I tested that, and it seemed to work, but I need to persist just
the code String into the model, not the USState object.
But what you could do is for example make getUSState() object on youre model
that is looked up by for example hibernate as a 1-> N relation and code is the key..
Then you can do setUSState(USState) on youre object but the value stored to the database is ofcourse just the code
Perhaps I don't understand the intended usage here.
It seems like the type of thing I want to do must be fairly common. Say I
want to list US States by full name, but persist the abbreviation (code)
in my model. A map of Strings in the front end (display, value) to a
String in the model (value). Is there a simple way to do this?
Pretty simple
first:
What is youre selected object?
this is the code because you don't have setUSState/getUSState as explained above.
then make a list of the same things that can be selected, so in youre case the codes
Then make a choiceRenderer:
class StateChoiceRender
{
Map codesToStates
getDisplayValue(Object code, int index)
{
// the object is one of the code strings. you want to display the name so in this
return ((USStates)codesToNames.get(code)).getName();
}
getIdValue(Object code)
{
// the code string is directly the id value:
return code;
}
}
new DropDownChoice("usstate", LIST_OF_CODES, new StateChoiceRenderer())
then it works.