• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

html:select and html:options simple question

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post Struts questions in the Struts forum. This has been moved there.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your JSP should look like this:

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

Here is a link that explains how to use these tags.
[ April 13, 2006: Message edited by: Merrill Higginson ]
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
changing this line
request.getSession().getServletContext().setAttribute("users", usersList);
to request.setAttribute("users", usersList); will work.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Would you like to try a free sample? Today we are featuring tiny ads:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic