• Post Reply Bookmark Topic Watch Topic
  • New Topic

Populate bean for use with JSTL

 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, while reading about JSTL I got little confused on how I would actually populate a bean. Usually... I do this with scriplets...


Vector profileList = new ArrayList();

profileList = DAO.getProfiles();


for (int i=0; i<profileList.size(); i++)
{
SomeBean myBean = profileList.elementAt(i);
out.println(myBean.getUsername());
out.println("<br");
}


Now I know this would be easier if I used the forEach loop of JSTL... The problem is... I don't know how I would actually fill in the value it iterates...


<c:forEach var="profiles" items="whatDoIPutHere???">
//print the username attribute in the bean
<br>
</c:forEach>

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ideally, you shouldn't be populating beans from within the view tier (JSPs).
Your beans should be populated from within a servlet or Model bean.
 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok... Here's the idea... I have a JSP page that has a drop down menu... Or a Drop down listbox if that's what to call it... I want the listbox populated from my database and usually... I would use a DAO, put the results in a Vector, and loop through that Vector and put each element in a bean... Is there any better way of doing this? I would appreciate some code samples. Thanks!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One approach would be for your DAO object to return a list of rowBeans.
The list could then be bound to request scope and parsed with JSTL tags.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Timothy Sam:
Ok... Here's the idea... I have a JSP page that has a drop down menu... Or a Drop down listbox if that's what to call it... I want the listbox populated from my database and usually... I would use a DAO, put the results in a Vector, and loop through that Vector and put each element in a bean... Is there any better way of doing this? I would appreciate some code samples. Thanks!


Although, Ben's answer is quite complete and straight. But I would like to be a bit more specific to what you are doing. Dont loop through that Vector. You should rather loop through your ResultSet object, place values in your bean/TO/VO property, then add that bean to your collection, in your case Vector.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!