Hi,
Depending on what you are trying to do or the scale of the application
you are building, you can declare the collection within your jsp page
and put it within the pageContext or you can declare it in your Action
subclass and put it in the session. I found the last option to be a
neater and better way of doing things. I think it will be a good idea to
declare / define the collection in the Action subclass, incase you need
to populate the list from an external source, such as a database.
Example 1 (within jsp page, taken from struts example application - subscription.jsp)
<%
java.util.ArrayList list = new java.util.ArrayList();
list.add(new org.apache.struts.webapp.example.LabelValueBean("IMAP Protocol", "imap"));
list.add(new org.apache.struts.webapp.example.LabelValueBean("POP3 Protocol", "pop3"));
pageContext.setAttribute("serverTypes", list);
%>
........ other code ......
<html:select property="type">
<html:options collection="serverTypes" property="value"
labelProperty="label"/>
</html:select>
........ other code ......
* The LabelValueBean is a utility class, which is part of the struts example,
that creates a bean object. The object contains label value pairs, as the
name implies.
Example 2 ( within Action subclass )
perform method - declared locally (avoid the use of instance
variables in Action class).
ArrayList list = new ArrayList();
...... other code .......
serverTypes.add( new LabelValueBean( <label - string>, <value - string> ) );
session.setAttribute( "serverTypes", serverTypes );
...... other code .......jsp page.<html:select property="type">
<html:options collection="serverTypes" property="value" />
</html:select>
* labelProperty can be omitted if the label and value have the same value.
I hope this helps you.
Chiji
Get your FREE download of MSN Explorer at http://explorer.msn.com

