Casey Bragg wrote:
> Does anyone know how to use indexed properties?
>
> I want the elements of my HTML form to be able to set
> properties in a bean
> without having to specify the exact name of the property.
> [...]

I posted a response to a similar question last month. In that posting, I
suggested adding a couple of methods to the bean, one that just returned the
number of items, and one that would retrieve one item at a time. If I had to
do it again, I would probably make my bean implement the Enumeration
interface so the JSP could call hasMoreElements() and nextElement().

Here's a copy of my old response:

<%@ page import = "some.package.name.VpnHostsBean" %>

<jsp:useBean id="vpnHosts"
        class="some.package.name.VpnHostsBean" scope="session" />


<HTML>
<HEAD>
...
<TABLE BORDER=0 bgcolor="#336699" CELLPADDING=5 CELLSPACING=2 WIDTH=100%>

<% for (int i = 0; i < vpnHosts.getNumHosts(); i++ ) { %>
        <tr><td bgcolor='#6699CC' width=150 align=right><font face='arial,
san-serif' color='#FFFFFF'>
        <%= vpnxHosts.getName(i) %>
        </font></td>
   <td bgcolor='#DDDDDD' width=350 align=left><font face='arial, san-serif'
color='#000000'>
        <%= vpnHosts.getAddress(i)  %>
   </font></td></tr>
<% } %>

</TABLE>
...

Here's part of the associated bean:
package some.package.name;

import java.util.*;
import java.io.*;


public class VpnHostsBean extends java.lang.Object implements Runnable
{
...
        protected int numHosts = 0;
        public int getNumHosts()        {
                numHosts = addresses.size();
                return numHosts;
        }

        protected Vector names = new Vector();
        public String getName(int i)    {
                return (String)names.elementAt(i);
        }
        protected void setName(int i, String value) {
                if (i >= names.size())
                        names.setSize(i + 1);
                names.setElementAt(value, i);
        }

        protected Vector addresses = new Vector();
        public String getAddress(int i) {
                return (String)addresses.elementAt(i);
        }
        protected void setAddress(int i, String value) {
                if (i >= addresses.size())
                        addresses.setSize(i + 1);
                addresses.setElementAt(value, i);
        }
...
}

Hope that helps,

Larry

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html

Reply via email to