Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

html:select and html:options simple question

 
Jas Oberai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • 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 ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post Struts questions in the Struts forum. This has been moved there.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Satish SN
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • 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
 
sweta naidu
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 ]
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic