Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

populating a list box with an array of strings 1

Status
Not open for further replies.

greygirl

Programmer
Jun 12, 2002
34
0
0
US
Help!!!
I have a struts app where i retrieve a string[] of years from database and with set property set it to a selectyears variable. In my jsp page I have a select box which I am populating it with these values.
In my form bean i have a getSelectyears and setSelectyears methods. getSelectyears returns a string[] of years.
In my jsp how do I populate my select box with options these years? Please help!!
greygirl
 
Please anybody! Any help is appreciated!!
 
The easiest way is to use <html:eek:ptions> tag. But it only accepts Collection for options value/labels, but not array. So you will have to covert the String array to List. Here is an example, I put the code in JSP for easy demonstration, you may want to move some code to the Action Class.


Code:
<%
   String [] selectYearsOptions = new String[] { "a", "b" , "c"};
   request.setAttribute("selectYearsOptions",Arrays.asList(selectYearsOptions));
%>
<html:form action="/yourFormAction">

<html:select property="selectYears" multiple="true">
  <html:options name="selectYearsOptions"/>
</html:select>

</html:form>
 
Hi byam
Thanks for answering and it works. If i have a list instead of an array of strings to begin with and the list is set in the bean as selectYears then do I still have to set the attribute to the request object? What can be the value, label of the options? Because I have a javascript which will add the options selected by the user to another list box.
Thanks
greygirl
 

The reason why I use array and turn that into a List was you mentioned that your data come in as an array.

If you already have a List for selectYears set as bean, then you don't need to turn that into array and back to a List again. Just use the List it in the option tag. i.e.

Code:
<html:select property="selectYears" multiple="true">
  <html:options name="selectYears"/>
</html:select>
 
Thanks byam. That fixes that. one more question to you. I also have an arraylist of object arrays (each array has 3 objects). I want to know how to iterate the rows with the logic:iterate tag in a table. Should I be using nested tags?
This is how it is constructed
List datalist = new ArrayList();
Object[] columns = new Object[];
with a for loop I added the data to object array. After one row of data is added then I added it to the list like this:
datalist.add(columns);
thanks again
greygirl
 
I don't think you cannot use nest tag for that.

If you want to use Struts tag to loop through all the item, you would have to use List for "columns" instead of Object[]. So you end having List of List instead of List of arrays.

Assuming you have the datalist as a bean named datalist, containing List of List, then you can do this:

Code:
<table>
<logic:iterate id="column" name="datalist">
<tr>
   <logic:iterate id="cell" name="column">
     <td><bean:write name="cell" /></td>
   </logic:iterate>
</tr>
</logic:iterate>
<table>
 
Thanks byam. But the list was displayed unordered (even though I arranged it matching the field name). In this application I access the database constantly and retrieve records and display. The records are of several columns and rows. It is wonderful to put the columns of each row in a list and in turn put the row(list) in another list. It is easy to iterate. But it comes as unordered. If you have done web apps accessing database can you advice me on the best form of collection objects to save these records queried from database so that they can be displayed with ease.
I am a single programmer working on a java app in our group (evertybodyelse is doing .NET) You have been extremely helpful. Thank you again.
 
I would imagine that a table row would be equal to a row of resultSet from databse. In this case, I would wrap a row by an object. Say a row of data represents a user profile. So you wrap those info into a User object, then you have a list of user Object, and can render into table as follows:

Let say List of User Object is stored in a bean "userList"
the list is created in the order of the result set return from database (assuming you use database to order the list). And User object has property: name, email, city.

Code:
<table>
<logic:iterate id="user" name="userList">
  <tr>
     <td><bean:write name="user" property="name"/></td>
     <td><bean:write name="user" property="email"/></td>
     <td><bean:write name="user" property="city"/></td>
  </tr>
</logic:iterate>
</table>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top