Martin,

Can I get a simple example bean and markup of its various options, so I 
can see how I have to get it nested?...

Ta.

Arron.

[EMAIL PROTECTED] wrote:

>martinc     02/02/22 23:10:30
>
>  Modified:    doc/userGuide struts-html.xml
>               src/exercise-taglib/org/apache/struts/webapp/exercise
>                        TestBean.java
>               src/share/org/apache/struts/taglib/html
>                        LocalStrings.properties
>               web/exercise-taglib html-select.jsp
>  Added:       src/share/org/apache/struts/taglib/html
>                        OptionsCollectionTag.java
>  Log:
>  Add new <html:optionsCollection> tag, as discussed last November.
>  
>  I don't particularly like the tag name, but I couldn't think of anything
>  better.
>  
>  Revision  Changes    Path
>  1.2       +421 -1    jakarta-struts/doc/userGuide/struts-html.xml
>  
>  Index: struts-html.xml
>  ===================================================================
>  RCS file: /home/cvs/jakarta-struts/doc/userGuide/struts-html.xml,v
>  retrieving revision 1.1
>  retrieving revision 1.2
>  diff -u -r1.1 -r1.2
>  --- struts-html.xml  20 Feb 2002 00:41:14 -0000      1.1
>  +++ struts-html.xml  23 Feb 2002 07:10:30 -0000      1.2
>  @@ -3430,7 +3430,7 @@
>                   element.  This tag can be used multiple times within a single
>                   <code>&lt;html:select&gt;</code> element, either in conjunction
>                   with or instead of one or more <code>&lt;html:option&gt;</code>
>  -                elements.</p>
>  +                or <code>&lt;html:optionsCollection&gt;</code> elements.</p>
>   
>                   <p>This tag operates in one of two major modes, depending on
>                   whether or not the <code>collection</code> attribute is
>  @@ -3570,6 +3570,426 @@
>               <rtexprvalue>true</rtexprvalue>
>               <info>
>                   CSS stylesheet class to be applied to this HTML element.
>  +            </info>
>  +        </attribute>
>  +    </tag>
>  +
>  +    <tag>
>  +
>  +        <name>optionsCollection</name>
>  +        <summary>
>  +            Render a Collection of Select Options
>  +        </summary>
>  +        <tagclass>org.apache.struts.taglib.html.OptionsCollectionTag</tagclass>
>  +        <bodycontent>empty</bodycontent>
>  +        <info>
>  +            <p>Renders a set of HTML <code>&lt;option&gt;</code> elements,
>  +            representing possible choices for a <code>&lt;select&gt;</code>
>  +            element.  This tag can be used multiple times within a single
>  +            <code>&lt;html:select&gt;</code> element, either in conjunction
>  +            with or instead of one or more <code>&lt;html:option&gt;</code>
>  +            or <code>&lt;html:options&gt;</code> elements.</p>
>  +
>  +            <p>This tag operates on a collection of beans, where each bean
>  +            has a <strong>label</strong> property and a <strong>value</strong>
>  +            property. The actual names of these properties can be configured
>  +            using the <code>label</code> and <code>value</code> attributes
>  +            of this tag.</p>
>  +
>  +            <p>This tag differs from the <code>&lt;html:options&gt;</code> tag
>  +            in that it makes more consistent use of the <code>name</code> and
>  +            <code>property</code> attributes, and allows the collection to be
>  +            more easily obtained from the enclosing form bean.</p>
>  +        </info>
>  +
>  +        <attribute>
>  +            <name>label</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                The property of the bean within the collection which represents
>  +                the label to be rendered for each option. Defaults to "label".
>  +            </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +            <name>name</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                The attribute name of the bean whose properties are consulted
>  +                when rendering the current value of this input field. If not
>  +                specified, the bean associated with the form tag we are nested
>  +                within is utilized.
>  +            </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +            <name>property</name>
>  +            <required>true</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                The property of the form bean, or the bean specified by the name
>  +                attribute, that will return the collection of objects to be
>  +                rendered for these options.
>  +            </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +            <name>style</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                CSS styles to be applied to this HTML element.
>  +            </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +            <name>styleClass</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                CSS stylesheet class to be applied to this HTML element.
>  +            </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +            <name>value</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                The property of the bean within the collection which represents
>  +                the value to be rendered for each option. Defaults to "value".
>  +            </info>
>  +        </attribute>
>  +    </tag>
>  +
>  +    <tag>
>  +
>  +        <name>password</name>
>  +        <summary>
>  +            Render A Password Input Field
>  +        </summary>
>  +        <tagclass>org.apache.struts.taglib.html.PasswordTag</tagclass>
>  +        <info>
>  +            Renders an HTML &lt;input&gt; element of type password, populated
>  +            from the specified value or the specified property of the bean
>  +            associated with our current form. This tag is only valid when
>  +            nested inside a form tag body.
>  +        </info>
>  +
>  +        <attribute>
>  +            <name>accesskey</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                The keyboard character used to move focus immediately to this
>  +                element.
>  +            </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +                <name>alt</name>
>  +                <required>false</required>
>  +                <rtexprvalue>true</rtexprvalue>
>  +                <info>
>  +                <p>The alternate text for this element.</p>
>  +                </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +                <name>altKey</name>
>  +                <required>false</required>
>  +                <rtexprvalue>true</rtexprvalue>
>  +                <info>
>  +                <p>The message resources key of the alternate text for this
>  +                element.</p>
>  +                </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +          <name>disabled</name>
>  +          <required>false</required>
>  +          <rtexprvalue>true</rtexprvalue>
>  +          <info>
>  +          Set to <code>true</code> if this input field should be
>  +          disabled.
>  +          </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +            <name>indexed</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                Valid only inside of logic:iterate tag.
>  +                If yes then name of the html tag will be rendered as
>  +                "id[34].propertyName". Number in brackets will be generated
>  +                for every iteration and taken from ancestor logic:iterate tag.
>  +            </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +            <name>maxlength</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                Maximum number of input characters to accept. [No limit]
>  +            </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +            <name>name</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                The attribute name of the bean whose properties are consulted when
>  +                rendering the current value of this input field. If not specified,
>  +                the bean associated with the form tag we are nested within is 
>utilized.
>  +            </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +            <name>onblur</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                JavaScript event handler executed when this element loses input
>  +                focus.
>  +            </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +            <name>onchange</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                JavaScript event handler executed when this element loses input
>  +                focus and its value has changed.
>  +            </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +            <name>onclick</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                JavaScript event handler executed when this element receives a
>  +                mouse click.
>  +            </info>
>  +        </attribute>
>  +
>  +
>  +        <attribute>
>  +            <name>ondblclick</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                JavaScript event handler executed when this element receives a
>  +                mouse double click.
>  +            </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +            <name>onfocus</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                JavaScript event handler executed when this element receives input
>  +                focus.
>  +            </info>
>  +        </attribute>
>  +
>  +
>  +        <attribute>
>  +            <name>onkeydown</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                JavaScript event handler executed when this element has focus and a
>  +                key is depressed.
>  +            </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +            <name>onkeypress</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                JavaScript event handler executed when this element has focus and a
>  +                key is depressed and released.
>  +            </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +            <name>onkeyup</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                JavaScript event handler executed when this element has focus and a
>  +                key is released.
>  +            </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +            <name>onmousedown</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                JavaScript event handler executed when this element is under the 
>mouse
>  +                pointer and a mouse button is depressed.
>  +            </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +            <name>onmousemove</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                JavaScript event handler executed when this element is under the
>  +                mouse pointer and the pointer is moved.
>  +            </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +            <name>onmouseout</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                JavaScript event handler executed when this element was under the
>  +                mouse pointer but the pointer was moved outside the element.
>  +            </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +            <name>onmouseover</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                JavaScript event handler executed when this element was not under
>  +                the mouse pointer but the pointer is moved inside the element.
>  +            </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +            <name>onmouseup</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                JavaScript event handler executed when this element is under the
>  +                mouse pointer and a mouse button is released.
>  +            </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +            <name>property</name>
>  +            <required>true</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                Name of the request parameter that will be included with this
>  +                submission, set to the specified value.
>  +            </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +          <name>readonly</name>
>  +          <required>false</required>
>  +          <rtexprvalue>true</rtexprvalue>
>  +          <info>
>  +          Set to <code>true</code> if this input field should be
>  +          read only.
>  +          </info>
>  +        </attribute>
>  +
>  +                <attribute>
>  +                        <name>redisplay</name>
>  +                        <required>false</required>
>  +                        <rtexprvalue>true</rtexprvalue>
>  +                        <info>
>  +                        Boolean flag indicating whether or not existing values
>  +                        will be redisplayed if they exist.  Even though the
>  +                        redisplayed value will be shown as asterisks on the
>  +                        visible HTML page, the cleartext of the actual password
>  +                        value will be visible though the "Show Page Source"
>  +                        menu option of the client browser.  You may wish to
>  +                        set this value to <code>false</code> on login pages.
>  +                        Defaults to <code>true</code> for consistency with
>  +                        all other form tags that redisplay their contents.
>  +                        </info>
>  +                </attribute>
>  +
>  +        <attribute>
>  +            <name>style</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                CSS styles to be applied to this HTML element.
>  +            </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +            <name>styleClass</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                CSS stylesheet class to be applied to this HTML element.
>  +            </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +            <name>styleId</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                Identifier to be assigned to this HTML element.
>  +            </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +            <name>size</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                Number of character positions to allocate. [Browser default]
>  +            </info>
>  +        </attribute>
>  +
>  +        <attribute>
>  +            <name>tabindex</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                The tab order (ascending positive integers) for this element.
>  +            </info>
>  +        </attribute>
>  +
>  +                <attribute>
>  +                  <name>title</name>
>  +                  <required>false</required>
>  +                  <rtexprvalue>true</rtexprvalue>
>  +                  <info>
>  +                  <p>The advisory title for this element.</p>
>  +                  </info>
>  +                </attribute>
>  +
>  +                <attribute>
>  +                  <name>titleKey</name>
>  +                  <required>false</required>
>  +                  <rtexprvalue>true</rtexprvalue>
>  +                  <info>
>  +                  <p>The message resources key for the advisory title
>  +                  for this element.</p>
>  +                  </info>
>  +                </attribute>
>  +
>  +        <attribute>
>  +            <name>value</name>
>  +            <required>false</required>
>  +            <rtexprvalue>true</rtexprvalue>
>  +            <info>
>  +                Value of the label to be placed on this button. This value will
>  +                also be submitted as the value of the specified request parameter.
>  +                [Body of this tag (if any), or "Cancel"]
>               </info>
>           </attribute>
>       </tag>
>  
>  
>  
>  1.5       +55 -4     
>jakarta-struts/src/exercise-taglib/org/apache/struts/webapp/exercise/TestBean.java
>  
>  Index: TestBean.java
>  ===================================================================
>  RCS file: 
>/home/cvs/jakarta-struts/src/exercise-taglib/org/apache/struts/webapp/exercise/TestBean.java,v
>  retrieving revision 1.4
>  retrieving revision 1.5
>  diff -u -r1.4 -r1.5
>  --- TestBean.java    15 Oct 2001 05:50:10 -0000      1.4
>  +++ TestBean.java    23 Feb 2002 07:10:30 -0000      1.5
>  @@ -1,7 +1,7 @@
>   /*
>  - * $Header: 
>/home/cvs/jakarta-struts/src/exercise-taglib/org/apache/struts/webapp/exercise/TestBean.java,v
> 1.4 2001/10/15 05:50:10 martinc Exp $
>  - * $Revision: 1.4 $
>  - * $Date: 2001/10/15 05:50:10 $
>  + * $Header: 
>/home/cvs/jakarta-struts/src/exercise-taglib/org/apache/struts/webapp/exercise/TestBean.java,v
> 1.5 2002/02/23 07:10:30 martinc Exp $
>  + * $Revision: 1.5 $
>  + * $Date: 2002/02/23 07:10:30 $
>    *
>    * ====================================================================
>    *
>  @@ -63,22 +63,73 @@
>   package org.apache.struts.webapp.exercise;
>   
>   
>  +import java.util.Collection;
>  +import java.util.Vector;
>   import javax.servlet.http.HttpServletRequest;
>   import org.apache.struts.action.ActionForm;
>   import org.apache.struts.action.ActionMapping;
>  +import org.apache.struts.util.LabelValueBean;
>   
>   
>   /**
>    * General purpose test bean for Struts custom tag tests.
>    *
>    * @author Craig R. McClanahan
>  - * @version $Revision: 1.4 $ $Date: 2001/10/15 05:50:10 $
>  + * @author Martin F N Cooper
>  + * @version $Revision: 1.5 $ $Date: 2002/02/23 07:10:30 $
>    */
>   
>   public class TestBean extends ActionForm {
>   
>   
>       // ------------------------------------------------------------- Properties
>  +
>  +
>  +    /**
>  +     * A collection property where the elements of the collection are
>  +     * of type <code>LabelValueBean</code>.
>  +     */
>  +    private Collection beanCollection = null;
>  +
>  +    public Collection getBeanCollection() {
>  +        if (beanCollection == null) {
>  +            Vector entries = new Vector(10);
>  +
>  +            entries.add(new LabelValueBean("Label 0", "Value 0"));
>  +            entries.add(new LabelValueBean("Label 1", "Value 1"));
>  +            entries.add(new LabelValueBean("Label 2", "Value 2"));
>  +            entries.add(new LabelValueBean("Label 3", "Value 3"));
>  +            entries.add(new LabelValueBean("Label 4", "Value 4"));
>  +            entries.add(new LabelValueBean("Label 5", "Value 5"));
>  +            entries.add(new LabelValueBean("Label 6", "Value 6"));
>  +            entries.add(new LabelValueBean("Label 7", "Value 7"));
>  +            entries.add(new LabelValueBean("Label 8", "Value 8"));
>  +            entries.add(new LabelValueBean("Label 9", "Value 9"));
>  +
>  +            beanCollection = entries;
>  +        }
>  +
>  +        return (beanCollection);
>  +    }
>  +
>  +    public void setBeanCollection(Collection beanCollection) {
>  +        this.beanCollection = beanCollection;
>  +    }
>  +
>  +
>  +    /**
>  +     * A multiple-String SELECT element using a bean collection.
>  +     */
>  +    private String[] beanCollectionSelect = { "Value 1", "Value 3",
>  +                                              "Value 5" };
>  +
>  +    public String[] getBeanCollectionSelect() {
>  +        return (this.beanCollectionSelect);
>  +    }
>  +
>  +    public void setBeanCollectionSelect(String beanCollectionSelect[]) {
>  +        this.beanCollectionSelect = beanCollectionSelect;
>  +    }
>   
>   
>       /**
>  
>  
>  
>  1.14      +5 -2      
>jakarta-struts/src/share/org/apache/struts/taglib/html/LocalStrings.properties
>  
>  Index: LocalStrings.properties
>  ===================================================================
>  RCS file: 
>/home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/LocalStrings.properties,v
>  retrieving revision 1.13
>  retrieving revision 1.14
>  diff -u -r1.13 -r1.14
>  --- LocalStrings.properties  13 Jan 2002 03:38:38 -0000      1.13
>  +++ LocalStrings.properties  23 Feb 2002 07:10:30 -0000      1.14
>  @@ -22,6 +22,7 @@
>   imgTag.type=Object must be of type Map
>   includeTag.include=Error including page {0}: {1}
>   includeTag.lookup=Cannot find global forward named {0}
>  +indexed.noEnclosingIterate=indexed=\"true\" is only valid within an enclosing 
>iterate tag
>   iterateTag.iterator=Cannot create iterator for {0}
>   linkTag.destination=You must specify exactly one of 'forward', 'href', or 'page'
>   linkTag.forward=Cannot locate global forwarding for {0}
>  @@ -32,9 +33,11 @@
>   messageTag.message=Missing message for key {0}
>   messageTag.resources=Missing resources attribute {0}
>   multiboxTag.value=You must specify the value attribute or nested tag content
>  -optionTag.select=Option tag must be nested in a Select tag
>  +optionsCollectionTag.collection=Failed to obtain specified collection
>  +optionsCollectionTag.iterator=Cannot create iterator for {0}
>  +optionsCollectionTag.select=OptionsCollection tag must be nested in a Select tag
>   optionsTag.enumeration=Cannot create enumeration for {0}
>   optionsTag.iterator=Cannot create iterator for {0}
>   optionsTag.select=Options tag must be nested in a Select tag
>  +optionTag.select=Option tag must be nested in a Select tag
>   rewrite.url=Cannot create rewrite URL: {0}
>  -indexed.noEnclosingIterate=indexed=\"true\" is only valid within an enclosing 
>iterate tag
>  
>  
>  
>  1.1                  
>jakarta-struts/src/share/org/apache/struts/taglib/html/OptionsCollectionTag.java
>  
>  Index: OptionsCollectionTag.java
>  ===================================================================
>  /*
>   * ====================================================================
>   *
>   * The Apache Software License, Version 1.1
>   *
>   * Copyright (c) 2002 The Apache Software Foundation.  All rights
>   * reserved.
>   *
>   * Redistribution and use in source and binary forms, with or without
>   * modification, are permitted provided that the following conditions
>   * are met:
>   *
>   * 1. Redistributions of source code must retain the above copyright
>   *    notice, this list of conditions and the following disclaimer.
>   *
>   * 2. Redistributions in binary form must reproduce the above copyright
>   *    notice, this list of conditions and the following disclaimer in
>   *    the documentation and/or other materials provided with the
>   *    distribution.
>   *
>   * 3. The end-user documentation included with the redistribution, if
>   *    any, must include the following acknowlegement:
>   *       "This product includes software developed by the
>   *        Apache Software Foundation (http://www.apache.org/)."
>   *    Alternately, this acknowlegement may appear in the software itself,
>   *    if and wherever such third-party acknowlegements normally appear.
>   *
>   * 4. The names "The Jakarta Project", "Struts", and "Apache Software
>   *    Foundation" must not be used to endorse or promote products derived
>   *    from this software without prior written permission. For written
>   *    permission, please contact [EMAIL PROTECTED]
>   *
>   * 5. Products derived from this software may not be called "Apache"
>   *    nor may "Apache" appear in their names without prior written
>   *    permission of the Apache Group.
>   *
>   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
>   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
>   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
>   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
>   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
>   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
>   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
>   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
>   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
>   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
>   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
>   * SUCH DAMAGE.
>   * ====================================================================
>   *
>   * This software consists of voluntary contributions made by many
>   * individuals on behalf of the Apache Software Foundation.  For more
>   * information on the Apache Software Foundation, please see
>   * <http://www.apache.org/>.
>   *
>   */
>  
>  package org.apache.struts.taglib.html;
>  
>  import java.io.IOException;
>  import java.lang.reflect.InvocationTargetException;
>  import java.util.Arrays;
>  import java.util.Collection;
>  import java.util.Enumeration;
>  import java.util.Iterator;
>  import java.util.Map;
>  import javax.servlet.jsp.JspException;
>  import javax.servlet.jsp.JspWriter;
>  import javax.servlet.jsp.PageContext;
>  import javax.servlet.jsp.tagext.TagSupport;
>  import org.apache.commons.beanutils.PropertyUtils;
>  import org.apache.struts.util.IteratorAdapter;
>  import org.apache.struts.util.MessageResources;
>  import org.apache.struts.util.RequestUtils;
>  import org.apache.struts.util.ResponseUtils;
>  
>  
>  /**
>   * Tag for creating multiple &lt;select&gt; options from a collection. The
>   * collection may be part of the enclosing form, or may be independent of
>   * the form. Each element of the collection must expose a 'label' and a
>   * 'value', the property names of which are configurable by attributes of
>   * this tag.
>   * <p>
>   * The collection may be an array of objects, a Collection, an Enumeration,
>   * an Iterator, or a Map.
>   * <p>
>   * <b>NOTE</b> - This tag requires a Java2 (JDK 1.2 or later) platform.
>   *
>   * @author Martin Cooper
>   * @version $Revision: 1.1 $ $Date: 2002/02/23 07:10:30 $
>   */
>  
>  public class OptionsCollectionTag extends TagSupport {
>  
>  
>      // ----------------------------------------------------- Instance Variables
>  
>  
>      /**
>       * The message resources for this package.
>       */
>      protected static MessageResources messages =
>              MessageResources.getMessageResources(
>                  Constants.Package + ".LocalStrings");
>  
>  
>      // ------------------------------------------------------------- Properties
>  
>  
>      /**
>       * The name of the bean property containing the label.
>       */
>      protected String label = "label";
>  
>      public String getLabel() {
>          return label;
>      }
>  
>      public void setLabel(String label) {
>          this.label = label;
>      }
>  
>  
>      /**
>       * The name of the bean containing the values collection.
>       */
>      protected String name = Constants.BEAN_KEY;
>  
>      public String getName() {
>          return name;
>      }
>  
>      public void setName(String name) {
>          this.name = name;
>      }
>  
>  
>      /**
>       * The name of the property to use to build the values collection.
>       */
>      protected String property = null;
>  
>      public String getProperty() {
>          return property;
>      }
>  
>      public void setProperty(String property) {
>          this.property = property;
>      }
>  
>  
>      /**
>       * The style associated with this tag.
>       */
>      private String style = null;
>  
>      public String getStyle() {
>          return style;
>      }
>  
>      public void setStyle(String style) {
>          this.style = style;
>      }
>  
>  
>      /**
>       * The named style class associated with this tag.
>       */
>      private String styleClass = null;
>  
>      public String getStyleClass() {
>          return styleClass;
>      }
>  
>      public void setStyleClass(String styleClass) {
>          this.styleClass = styleClass;
>      }
>  
>  
>      /**
>       * The name of the bean property containing the value.
>       */
>      protected String value = "value";
>  
>      public String getValue() {
>          return value;
>      }
>  
>      public void setValue(String value) {
>          this.value = value;
>      }
>  
>  
>      // --------------------------------------------------------- Public Methods
>  
>  
>      /**
>       * Process the start of this tag.
>       *
>       * @exception JspException if a JSP exception has occurred
>       */
>      public int doStartTag() throws JspException {
>  
>          // Acquire the select tag we are associated with
>          SelectTag selectTag =
>                  (SelectTag)pageContext.getAttribute(Constants.SELECT_KEY);
>  
>          if (selectTag == null) {
>              JspException e = new JspException(
>                      messages.getMessage("optionsCollectionTag.select"));
>              RequestUtils.saveException(pageContext, e);
>              throw e;
>          }
>  
>          // Acquire the collection containing our options
>          Object collection = RequestUtils.lookup(pageContext,
>                  name, property, null);
>  
>          if (collection == null) {
>              JspException e = new JspException(
>                      messages.getMessage("optionsCollectionTag.collection"));
>              RequestUtils.saveException(pageContext, e);
>              throw e;
>          }
>  
>          // Acquire an iterator over the options collection
>          Iterator iter = getIterator(collection);
>  
>          StringBuffer sb = new StringBuffer();
>  
>          // Render the options
>          while (iter.hasNext()) {
>  
>              Object bean = iter.next();
>              Object beanLabel = null;;
>              Object beanValue = null;
>  
>              // Get the label for this option
>              try {
>                  beanLabel = PropertyUtils.getProperty(bean, label);
>                  if (beanLabel == null) {
>                      beanLabel = "";
>                  }
>              } catch (IllegalAccessException e) {
>                  JspException jspe = new JspException(
>                          messages.getMessage("getter.access", label, bean));
>                  RequestUtils.saveException(pageContext, jspe);
>                  throw jspe;
>              } catch (InvocationTargetException e) {
>                  Throwable t = e.getTargetException();
>                  JspException jspe = new JspException(
>                          messages.getMessage("getter.result", label,
>                                  t.toString()));
>                  RequestUtils.saveException(pageContext, jspe);
>                  throw jspe;
>              } catch (NoSuchMethodException e) {
>                  JspException jspe = new JspException(
>                          messages.getMessage("getter.method", label, bean));
>                  RequestUtils.saveException(pageContext, jspe);
>                  throw jspe;
>              }
>  
>              // Get the value for this option
>              try {
>                  beanValue = PropertyUtils.getProperty(bean, value);
>                  if (beanValue == null) {
>                      beanValue = "";
>                  }
>              } catch (IllegalAccessException e) {
>                  JspException jspe = new JspException(
>                          messages.getMessage("getter.access", value, bean));
>                  RequestUtils.saveException(pageContext, jspe);
>                  throw jspe;
>              } catch (InvocationTargetException e) {
>                  Throwable t = e.getTargetException();
>                  JspException jspe = new JspException(
>                          messages.getMessage("getter.result", value,
>                                  t.toString()));
>                  RequestUtils.saveException(pageContext, jspe);
>                  throw jspe;
>              } catch (NoSuchMethodException e) {
>                  JspException jspe = new JspException(
>                          messages.getMessage("getter.method", value, bean));
>                  RequestUtils.saveException(pageContext, jspe);
>                  throw jspe;
>              }
>  
>              String stringLabel = beanLabel.toString();
>              String stringValue = beanValue.toString();
>  
>              // Render this option
>              addOption(sb, stringLabel, stringValue,
>                      selectTag.isMatched(stringValue));
>          }
>  
>          // Render this element to our writer
>          ResponseUtils.write(pageContext, sb.toString());
>  
>          return SKIP_BODY;
>  
>      }
>  
>  
>      /**
>       * Release any acquired resources.
>       */
>      public void release() {
>  
>          super.release();
>          label = null;
>          name = null;
>          property = null;
>          style = null;
>          styleClass = null;
>          value = null;
>  
>      }
>  
>  
>      // ------------------------------------------------------ Protected Methods
>  
>  
>      /**
>       * Add an option element to the specified StringBuffer based on the
>       * specified parameters.
>       *
>       * @param sb StringBuffer accumulating our results
>       * @param value Value to be returned to the server for this option
>       * @param label Value to be shown to the user for this option
>       * @param matched Should this value be marked as selected?
>       */
>      protected void addOption(StringBuffer sb, String label, String value,
>                               boolean matched) {
>  
>          sb.append("<option value=\"");
>          sb.append(value);
>          sb.append("\"");
>          if (matched)
>              sb.append(" selected=\"selected\"");
>          if (style != null) {
>              sb.append(" style=\"");
>              sb.append(style);
>              sb.append("\"");
>          }
>          if (styleClass != null) {
>              sb.append(" class=\"");
>              sb.append(styleClass);
>              sb.append("\"");
>          }
>          sb.append(">");
>          sb.append(ResponseUtils.filter(label));
>          sb.append("</option>\r\n");
>  
>      }
>  
>  
>      /**
>       * Return an iterator for the option labels or values, based on our
>       * configured properties.
>       *
>       * @param name Name of the bean attribute (if any)
>       * @param property Name of the bean property (if any)
>       *
>       * @exception JspException if an error occurs
>       */
>      protected Iterator getIterator(Object collection)
>              throws JspException {
>  
>          if (collection.getClass().isArray())
>              collection = Arrays.asList((Object[]) collection);
>  
>          if (collection instanceof Collection)
>              return (((Collection)collection).iterator());
>          else if (collection instanceof Iterator)
>              return ((Iterator)collection);
>          else if (collection instanceof Map)
>              return (((Map)collection).entrySet().iterator());
>          else if (collection instanceof Enumeration)
>              return(new IteratorAdapter((Enumeration)collection));
>          else
>              throw new JspException(
>                      messages.getMessage("optionsCollectionTag.iterator",
>                      collection.toString()));
>  
>      }
>  
>  }
>  
>  
>  
>  1.6       +10 -1     jakarta-struts/web/exercise-taglib/html-select.jsp
>  
>  Index: html-select.jsp
>  ===================================================================
>  RCS file: /home/cvs/jakarta-struts/web/exercise-taglib/html-select.jsp,v
>  retrieving revision 1.5
>  retrieving revision 1.6
>  diff -u -r1.5 -r1.6
>  --- html-select.jsp  21 Feb 2002 05:48:10 -0000      1.5
>  +++ html-select.jsp  23 Feb 2002 07:10:30 -0000      1.6
>  @@ -79,10 +79,19 @@
>     </tr>
>   
>     <tr>
>  -    <th align="right">Multiple Select From A Collection:</th>
>  +    <th align="right">Multiple Select From A Collection (Using 
>&lt;html:options&gt;):</th>
>       <td align="left">
>         <html:select property="collectionSelect" size="10" multiple="true">
>           <html:options collection="options" property="value" labelProperty="label"/>
>  +      </html:select>
>  +    </td>
>  +  </tr>
>  +
>  +  <tr>
>  +    <th align="right">Multiple Select From A Collection (Using 
>&lt;html:optionsCollection&gt;):</th>
>  +    <td align="left">
>  +      <html:select property="beanCollectionSelect" size="10" multiple="true">
>  +        <html:optionsCollection name="testbean" property="beanCollection"/>
>         </html:select>
>       </td>
>     </tr>
>  
>  
>  
>
>--
>To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
>For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
>



--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to