Is it possible to use an Object as value for a <html:select> (i'm using struts)

  • Thread starter =?iso-8859-1?q?Jos=E9_Antonio?=
  • Start date
?

=?iso-8859-1?q?Jos=E9_Antonio?=

Hi,

I'm having trouble with struts, well actually i don't know if the
problem is struts or instead i'm assuming html to be capable of
something it's not.

I have a ValidatorForm, say VF and one of it's attibutes called
organismo is an object of type Ob1, Ob1 being a bean definded in
another file, Ob1 has just two attributes a and b and their respective
setters and getters. I want that attribute to be filled with the value
of a <html:select>, depending on what the user chooses from the options
available.

To get the options i'm using the next:

<html:select property="organismo">
<html:eek:ptions collection="ListaOrganismos" property="id"
labelProperty="desFederacion"/>
</html:select>

where:
* organismo is the attribute (itself is a bean object) in my form
(struts validator form)
* ListaOrganismos is an ArrayList of objects (plain java beans), say
Ob0, with just two attributes: 1) id: an object of type Ob1,
2) desFederacion: a descriptive label

The problem is, when i submit the form, i cannot get the value of the
option (an Object of type Ob1 gotten from property="id") passed to the
next Action... if i try with primitives, there's no problem...
everything works as it should... but with an object... it just does not
work.

I have my mappings up an working... when i go to the next Action, i can
get all the other attributes from my form except for this one.

The labels are shown as i expected them to be shown... i drop the list
and there they are... that makes me think struts does understand the id
i give it... but just can't get it passed to the action...

Any idea of what could be happening? I'm thinking it's just i'm not
allowed to pass an object and i'm restricted to use primitives... but
i'm not sure...

I'll greatly apreciate if you guys could help me a little... i'm
stuck... thanks in advance.
 
W

Wendy Smoak

José Antonio said:
The problem is, when i submit the form, i cannot get the value of the
option (an Object of type Ob1 gotten from property="id") passed to the
next Action... if i try with primitives, there's no problem...
everything works as it should... but with an object... it just does not
work.

There are no objects in HTML forms... it's just text. The only thing the
browser is going to send back to the server is the name and value of each
element. (Except for file uploads, I suppose.)
I have a ValidatorForm, say VF and one of it's attibutes called
organismo is an object of type Ob1, Ob1 being a bean definded in
another file, Ob1 has just two attributes a and b and their respective
setters and getters. I want that attribute to be filled with the value
of a <html:select>, depending on what the user chooses from the options
available.

To get the options i'm using the next:

<html:select property="organismo">
<html:eek:ptions collection="ListaOrganismos" property="id"
labelProperty="desFederacion"/>
</html:select>

Try this:
<html:select property="organismo.a">

Struts (actually BeanUtils) will call form.getOrganismo().setA( ... ) with
whatever value was selected.

Then you have a small problem, because an HTML select list will only send
the 'value' attribute, which is usually an id of some kind. The text
description that's displayed in the list is not sent to the server.

But this isn't much of a problem-- after all, you created that list, so you
know what's in it. Once you know what value was chosen, you can look up the
description. (A Map might work better than a List here, it's easier to
retrieve the value you need.)

I only store the 'code' (your 'id') in my forms, then I have a bunch of Maps
sitting in Application scope that hold the values. I use the Maps both to
populate the select lists, and also to display the description of the
selected item in the 'confirmation' page.

HTH,
 
?

=?iso-8859-1?q?Jos=E9_Antonio?=

Thanks a lot Wendy... so it's not possible to set an Object as an ID in
a html form... mmm, i expected so...i was just hoping that not to be
that way.

The problem is, when a user chooses an option, i want that option to be
associated with a couple of values as ID, not just one string but two.

I could get the pair of values and concatenate the with some character,
handle them in the form as a unique string composed of the two values
and them split them in the Action that follows the jsp to have them as
two again... but i don't feel that to be right... but as it seems... i
have no choice.

Do i have a choice? Any suggestion?

Thanks once again.
 
W

Wendy Smoak

José Antonio said:
I could get the pair of values and concatenate the with some character,
handle them in the form as a unique string composed of the two values
and them split them in the Action that follows the jsp to have them as
two again... but i don't feel that to be right... but as it seems... i
have no choice.

You don't need the 'description' String to come in from the browser-- you're
the one who put it in the List that created the drop-down select list to
begin with. When the browser tells you what ID was selected, find the
description again from wherever you got it in the first place.

My Form beans don't have anything but Strings and a few booleans for
checkboxes. From those I populate objects with more complex types. IMO, It
sounds like you might be putting too much in the Form bean. It should be
fairly simple, its only job is to keep track of user input.
 
?

=?iso-8859-1?q?Jos=E9_Antonio?=

Yes, usually i do that and try to keep forms as simple as posible, but
this time i find it hard to do so with a couple of drop-down lists
because i populate them from a database where their key value is
composed of an ID an a date. So when i'm done with choosing which one
do i want, to really mean the option i chose, in the next step of the
flow i really need to get both the id and the date. With those values
i can take on an get the others, as the description or whatever, thats
true.

I generate the lists in the first place from a select with no
variables, i just get all the rows in a table. The natural way would be
to set the pair of values (the key) in an Object, but i cannot that.

Again, i must pass the key composed of two values and from then i would
restore the row from the database if needed.

Any way to do that?

Thanks in advance.
 
W

Wendy Smoak

José Antonio said:
Again, i must pass the key composed of two values

Any way to do that?

Various ways... your goal must be to get both of the Strings concatenated in
what becomes the 'value' of the HTML <option> tag.

You could provide an additional 'get' method in your bean that returns the
concatenated String, or else stop using <html:eek:ptions> and instead do the
iteration yourself, using <html:eek:ption>.

You might want to look at JSTL and the Struts-EL tags, using expressions in
the Struts <html:eek:ption> tag should make this job fairly easy.

Then you'll have to split the String into your two values, either with
clever 'get' methods in your form, or else in the Action when you need the
values.
 
?

=?iso-8859-1?q?Jos=E9_Antonio?=

I did it concatenating the values prior to sending them to the form,
and got them split with clever 'get' methods in the Action... it works
pretty well.

I'll also loot at JSTL to see what else can i do.

Thank you very much Wendy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top