File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes html:select and html:options Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "html:select and html:options" Watch "html:select and html:options" New topic

html:select and html:options

Mattia Merenda
Ranch Hand

Joined: Dec 14, 2006
Posts: 47
Hi everybody,
I am not able to solve my problem with the select tag.
I need to populate the select tag with the elements of a field in a table of my database.
Now,I am just developing a very simple application where I just want to show the result of the select tag.
So I wrote that is my ActionForm class like this:

class TestForm extends ActionForm
public String username;

public String getUsername() {
return username;

public void setUsername(String string) {

public TestForm() {
super(); }

public ActionMapping execute(....) {......}

The code of my Action class is:
class Test extends Action
private final static String SUCCESS="success";
private ArrayList users = new ArrayList();

public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest req,HttpServletResponse resp)
throws Exception {
HttpSession session = req.getSession(true);
users = getUsers();
if (users != null)
return mapping.findForward(SUCCESS);

public ArrayList getUsers() {
String sql;
ArrayList test = new ArrayList();
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/DBname?user=username&password=password");
sql = "SELECT DISTINCT(username) FROM table";
Prepared statement = con.prepareStatement(sql);
ResultSet rs = statement.executeQuery();
while ( {
return test;
<catch {.....}

In my struts-config.xml file I have this code:

<form-bean type="com.myapp.struts.TestForm" name="TestForm" />
<action type="com.myapp.struts.TestForm" path="/test" />

In the file index.jsp I use this code to use the <html:select> tag:

<logic:iterate id="test" collection="usersList" scope="session" type="com.myapp.struts.TestForm">
<html:select name="test" property="username" styleClass="dropdown">
<html ptions name="test" property=username">

when I run the application I get this error:

javax.servlet.ServletException:Cannot create iterator for this collection

How can I solve this problem?
Ajay Lulia

Joined: Jul 15, 2003
Posts: 2
Hi Mattia,

Use following code in your JSP, this might help you:

<html:select name="addressForm" property="currentStateCode" size="1">
<logic:iterate id="user" name="usersList">
<html: options name="test" property="user.username">
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I think the code example by Ajay gets you a little closer, but I don't think it solves your issue. I suspect that the error is caused by your use of the htmlptions tag which is designed to render an collection of select options. If you are using the iterate tag, then I think you want to use hmtlption (without the ending s). Or you can probably get rid of the iterate tag and use "usersList" for the name attribute on your htmlptions tag.

I have to admit that I almost always store such collections on the form and then use the htmlptionsCollection tag. Here is an example:

- Brent
Mattia Merenda
Ranch Hand

Joined: Dec 14, 2006
Posts: 47
I already tried Brent's advice before without success.I also tried Ajay's advice and it still does not work:
In particular I wrote:

<html:select name="TestForm" property="username" size="1">
<logic:iterate id="user" collection="usersList">
<html ptions name="test" property="user.username" />

and I found the error:Cannot find bean under name TestForm.

So I added at the code before the <html:select> tag the following tag
<jsp:usebean id="test" scope="session" class="com.myapp.struts.TestForm">

but I get the same error.
I am getting crazy with this problem.It is really awful.
Any other advice?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Remove the logic:iterate tag entirely from your JSP. The <htmlptions> tag already iterates over the list you give it. The following code should work:

For a detailed decription of how to use the <htmlptions> tag, see the "options" section in this link
[ December 21, 2006: Message edited by: Merrill Higginson ]

Consultant, Sima Solutions
Mattia Merenda
Ranch Hand

Joined: Dec 14, 2006
Posts: 47
I solved this problem for this simple application.
I put the ArrayList method getUsers from the Action class into the ActionForm class.In the JSP I used the code of Merill with this change:
<html:select name="prova" property="username" styleClass="dropdown" size="1" >
<html ptions name="prova" property="users" />

I hope it will help anybody else.
Now I must use what I learnt in a bigger application.
So,Thanks everybody.

I agree. Here's the link:
subject: html:select and html:options
It's not a secret anymore!