If I may jump in...I like to use the Iterator pattern for building select
lists.  While this may not be the best application for a situation where the
list of values is predetermined (such as with states), it works well when
reading from a variable list of values such as from a database.

In my bean I keep the list of values along with an index of the last one
retrieved.  My JSP code then contains something like:

<% while (pageBean.hasMoreStates()) { %>
  <option value=<%= pageBean.getStateAbbv() %>><%= pageBean.getStateName()
%></option>
<% } %>

Each call to hasMoreStates increments the counter and returns true if there
are still more in the list.  The calls to getStateAbbv and getStateName use
this counter to get the proper value from the list (usually a property of
another object held within an ArrayList).  Also, make sure you enclose
everything within the loop in curly braces.

If I need to pre-select a specific option, I add another call which returns
the string "selected" or an empty string, depending on if the iterator is
currently on the proper entry.

<% while (pageBean.hasMoreStates()) { %>
  <option value=<%= pageBean.getStateAbbv() %> <%=
pageBean.getStateSelected() %>><%= pageBean.getStateName() %></option>
<% } %>

HTH
Mark

-----Original Message-----
From: Denise Mangano [mailto:[EMAIL PROTECTED]]
Sent: Friday, January 03, 2003 1:58 PM
To: 'Tomcat Users List'
Subject: RE: Easy question - Java Beans


Noel.

How would your original select look for the states?

I have a variable in the bean for state which is the value (i.e. "NY") but
not one for the state name (i.e. "New York").  My select is basic: 
<select name="state">
   <option value="NY">New York</option>
</select>

In my bean I have private String state; with the getter and setter methods.

Thanks.

Denise Mangano
Help Desk Analyst
Complus Data Innovations, Inc.


-----Original Message-----
From: Noel J. Bergman [mailto:[EMAIL PROTECTED]] 
Sent: Thursday, January 02, 2003 6:04 PM
To: Tomcat Users List
Subject: RE: Easy question - Java Beans


Denise,

Start with your basic <select> tag, and then in your code that emits the
list of <option> tags, do something like this:

  <option value='" + stateCode + "'" + ((stateCode.equals(currentStateCode))
? " selected " : " ") + ">" + stateName + "</option>"

In other words, emit the

  <option value='NY'>New York</option>

tag, or the

  <option value='NY' selected>New York</option>

variant, depending upon whether the option you are about to emit is the
currently selected option.

        --- Noel


--
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]>

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

Reply via email to