• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

html:select and html:options

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 TestForm.java that is my ActionForm class like this:

class TestForm extends ActionForm
{
public String username;

public String getUsername() {
return username;
}

public void setUsername(String string) {
username=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)
session.setAtttribute("usersList"users);
return mapping.findForward(SUCCESS);
}

public ArrayList getUsers() {
String sql;
ArrayList test = new ArrayList();
try {
Class.forName("com.mysql.jdbc.Driver");
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 (rs.next()) {
test.add(rs.getString(1));
}
return test;
}
<catch {.....}
}

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

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

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">
</html:select>
</logic:iterate>

when I run the application I get this error:

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

How can I solve this problem?
Thanks,
Mattia
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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">
</logic:iterate>
</html:select>
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 html:options 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 hmtl:option (without the ending s). Or you can probably get rid of the iterate tag and use "usersList" for the name attribute on your html:options tag.

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


- Brent
 
Mattia Merenda
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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" />
</logic:iterate>
</html:select>

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?
Thanks,
Mattia
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remove the logic:iterate tag entirely from your JSP. The <html:options> tag already iterates over the list you give it. The following code should work:


For a detailed decription of how to use the <html:options> tag, see the "options" section in this link
[ December 21, 2006: Message edited by: Merrill Higginson ]
 
Mattia Merenda
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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" />
</html:select>

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

Mattia
 
reply
    Bookmark Topic Watch Topic
  • New Topic