Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

struts newbie question

Thread view: 
Greg White - 18 Apr 2007 19:32 GMT
I am currently reading "Struts Survival Guide" and doing some sample
coding - excellent book however one area that is not explained is populating
select box options. My question is for the following tag on a JSP page

<html:select name="customer>
<html:options collection="customers" value="id" label="name" />
</html:select>

Do I just create a class called customers that extends collection and put it
in my build path or am I missing some steps here. Do I need other
initialization in my JSP. If anyone can supply me with some sample code it
would be much appreciated.

Greg
Richard Senior - 18 Apr 2007 21:39 GMT
> <html:select name="customer>
> <html:options collection="customers" value="id" label="name" />
[quoted text clipped - 3 lines]
> put it in my build path or am I missing some steps here. Do I need other
> initialization in my JSP.

You shouldn't need to create a special class that implements the
Collection interface. I'm assuming you already have a bean to represent
a Customer. You can use an ArrayList to hold Customer objects for the
select list.

The two most common approaches are to:

a) Populate an ArrayList with your customers and place it in request scope.

b) Put an ArrayList on the associated form bean, which itself will be in
either request (or perhaps session) scope and available to your JSP.

Try option (a) first, but either way, the collection should be populated
in the action class that forwards to the JSP, not in the JSP itself. (If
you did that the view would be accessing the model directly, which is
not MVC). So, you'll have something like:

    ArrayList customers = new ArrayList();
    ...
    // Populate the list from the model ...
    ...
    request.setAttribute("customers", customers);
    return mapping.findForward("forwardToMyJsp");

Regards,

Richard


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.