wood burning stoves 2.0*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "html:select and html:options simple question" Watch "html:select and html:options simple question" New topic
Author

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.
Thanks
[ April 12, 2006: Message edited by: Bear Bibeault ]

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

Joined: Jan 10, 2002
Posts: 61428
    
  67

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


[Asking smart questions] [Bear's FrontMan] [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" />
</html:select>

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

Merrill
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();

while(rs.next())
{

userList.add(user.setLastName(rs.getString(1)));
}

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
Greenhorn

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.

Thanks
Mick
[ 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: http://aspose.com/file-tools
 
subject: html:select and html:options simple question