You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Johan Compagner <jc...@j-com.nl> on 2001/06/11 17:02:32 UTC

OptionsTag and SelectTag proposed changes

Hi,

I wanted to fill in a Select with Options that are filled out of a Collection.
where i could specify a value and label property of the objects that are in that connection.

Then i saw that this was already implemented bug only for Collection that are in the request.
But that was not the case at my place. Because the collection did came from the Form (where else??)

So in the OptionsTag.doEndTag() there is this line:

Iterator collIterator = getIterator(collection, null);

And that getIterator does this with that name (collection string)

Object bean = pageContext.findAttribute(beanName);

So it wants the collection in the page/request/session scope directy. But that is not what i want 
it want this:

collIterator = getIterator(name, collection);

Where the name is null (then the Constants.BEAN_KEY is used which is the form) or what every i want

So i changed it so that the current behaviour is excactly the same because it does if first as it was now
But if it can't find it it tries it my way.

I replaced the Iterator collIterator = getIterator(collection, null); line with these onces:

  Iterator collIterator = null;
  Object colBean = pageContext.findAttribute(collection);
  if (colBean == null)
  {
   collIterator = getIterator(name, collection);
  }
  else
  {
   collIterator = getIterator(collection, null);
  }


Now another problem arised. How do i get the right onces selected if i use a Collection?
Because how does the current selecttag know which where it must get the the right selected
values with which it should compare it with the value of the options (that is get from the collection)

Because the current behaviour is this:

That i calls the BeanUtils.getArrayProperty() for that and does a normal toString on that object
if it is not a collection and a toString of the values inside t he collection or array if it is such a thing.

But this is not what i want because it could just be an object (directly or inside a collection if multi select)
where it must call getPrimaryKey() to get the right value for example. (could be the same valueProperty
that the OptionsTag uses)

For now this doesn't have to be completely generic. I thought what must i change if i just extend the SelectTag
of struts it self. Then i only want to change the matching values.

At this time those are generated inside the doStartTag() so i must implement that complete doStartTag()
This is not very handy. Can the SelectTag be refactored so that the code for generating the match values
is in there own public/protected method so that i can only replace that one with my own?
Then the generation of the HMTL select is always the same.

i added a generateMatched() method in the SelectTag and where i put the code that is between the else:

 if (value != null)
 {
  match = new String[1];
  match[0] = value;
 }
 else
 {
        // ALL THIS CODE IS in this method:
        generateMatched();
 }

Can these 2 be added?
It doesn't change any current behaviour as far as i can see, so it shouldn't be to hard.

I attached my code with the changes.

Johan Compagner