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 simple question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "html:select and html:options simple question" Watch "html:select and html:options simple question" New topic

html:select and html:options simple question

Jas Oberai
Ranch Hand

Joined: Jan 16, 2005
Posts: 231
Hi Friends,
I am having a problem, please help me .I want to run a query and display all the lastnames of all the users in my database.Here are my files:

My problem is what i need to do in my Action class,so that it sets the "users" variable
properly and i would be able to read that variable in my jsp and display the lastname
of all the users.I am tryin this but failing badly:

Please Please help,I would really appreciate your help.
[ April 12, 2006: Message edited by: Bear Bibeault ]

SCJP 1.4 (88%)<br />SCWCD 1.4 (88%)
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63868

Please post Struts questions in the Struts forum. This has been moved there.

[Asking smart questions] [About Bear] [Books by Bear]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Your JSP should look like this:

<html:select property="lastName">
<htmlptions name="users" />

Here is a link that explains how to use these tags.
[ April 13, 2006: Message edited by: Merrill Higginson ]

Consultant, Sima Solutions
Satish SN
Ranch Hand

Joined: Apr 19, 2005
Posts: 70
Hi Jas !

By looking at ur code snippet i feel u should set the UserList object simply like this request.setAttribute("users",UserList);

it is the struts that will find out the users from the available scope u need not mention it explicitly.

and also i observed some flaw in ur jdbc code which builds the lastname into a arraylist

ArrayList userList = new ArrayList();



return userList;

In this way u can traverse through the whole resultset

i think u r code does not traverse through the whole resultset

Satish SN<br />SCJP 1.4 & SCWCD 1.4
sweta naidu

Joined: Nov 02, 2004
Posts: 25
changing this line
request.getSession().getServletContext().setAttribute("users", usersList);
to request.setAttribute("users", usersList); will work.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
There's nothing wrong with:

request.getSession().getServletContext().setAttribute("users", usersList);

He's just putting the bean in application scope rather than request scope. For a list that is the same for all users of the application it makes better sense than putting it in request scope.

When Struts tags are used in a JSP, they look for a bean in any scope, including application scope.
Jas Oberai
Ranch Hand

Joined: Jan 16, 2005
Posts: 231
Thanks a lot friends,i was really stuck on that one and I got it solved,but I think I am not doing it the right way,please correct me:

This is the method in my utility class where i run the query and I doubt I am not doing it right here:

And I made this constructor in my viewForm class:

I do this in my Action class then:

And finally this in my jsp:

And it works,but i don't think I can keep adding constructors to my form class like that,and moreoever if I want to search by first name ,there would be a conflict between 2 constructors,so can someone please tell me the right way to do this.

[ April 13, 2006: Message edited by: jas oberai ]
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Constructors work good for classes with just a few properties, but in your case it is probably best to create get and set methods and use those. Like this:

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