struts newbie question

G

Greg White

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:eek:ptions 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
 
R

Richard Senior

Greg said:
<html:select name="customer>
<html:eek:ptions 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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,629
Members
45,330
Latest member
AlvaStingl

Latest Threads

Top