This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

html:select and html:options

 
Mattia Merenda
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Ajay Lulia
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • 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>
 
Brent Sterling
Ranch Hand
Posts: 948
  • 0
  • Mark post as helpful
  • send pies
  • 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 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
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Mattia Merenda
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic