For a simple select like this one (months of the year), I use the following
code in my work. 

There is one file, AppStart.java, which contains a servlet that executes at
application start. The second part of the code shows what to add to web.xml
to make this execute. The last piece of code shows how to use the collection
in a jsp page.

Wiebe
http://frontierj.blogspot.com

--------------
AppStart.java:

package com.myco.myapp;

import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

import org.apache.struts.util.LabelValueBean;

public class AppStart extends HttpServlet {

// a static representation of the MONTHS table
  private LabelValueBean[] monthTable = {
    new LabelValueBean("January", "1"),
    new LabelValueBean("February", "2"),
    new LabelValueBean("March", "3"),
    new LabelValueBean("April", "4"),
    new LabelValueBean("May", "5"),
    new LabelValueBean("June", "6"),
    new LabelValueBean("July", "7"),
    new LabelValueBean("August", "8"),
    new LabelValueBean("September", "9"),
    new LabelValueBean("October", "10"),
    new LabelValueBean("November", "11"),
    new LabelValueBean("December", "12")
  };
 
  // this will execute at startup
  public void init() throws ServletException {

    // month list
    ArrayList monthList = new ArrayList();
    for (int i = 0; i < monthTable.length; i++)
      monthList.add(i, monthTable[i]);
    getServletContext().setAttribute("appMonthList", monthList);
  }
}

--------------
web.xml:

  <servlet>
    <servlet-name>appstart</servlet-name>
    <servlet-class>com.myco.myapp.AppStart</servlet-class>
    <init-param>
      <param-name>debug</param-name>
      <param-value>0</param-value>
    </init-param>
    <load-on-startup>5</load-on-startup>
  </servlet>

--------------
page.jsp:

<tr>
  <td>Month:</td>
  <td><html:select property="month">
    <html:options collection="appMonthList" property="value"
labelProperty="label"/>
  </html:select></td>
</tr>

--------------

-----Original Message-----
From: Bob Langford [mailto:[EMAIL PROTECTED] 
Sent: Saturday, October 11, 2003 10:35 AM
To: [EMAIL PROTECTED]
Subject: How can I compute data for html:select choices?

Hi,  I can't seem to understand how to declare things correctly to do
what I want to do.  Can anyone tell me where I am going wrong?

I'm trying to use a simple Java method as the source of the choices
in a html:select box in a form.  The method returns a java.util.List,
where each item is a org.apache.struts.util.ValueLabelBean object, just
what I need for this.  Here's one way I tried to use it:
=============
<%@ page import="foo.bar.MyUtils" %>
<jsp:useBean id="months" type="java.util.List" />
<%  months = MyUtils.getMonthsList();   %>
   ...
    <html:select  ... >
      <html:options collection="months" property="value" 
labelProperty="label" />
    </html:select>
=============
The problem is that "useBean" looks up the attribute "months" in the page
context, and since it can't find it, throws an exception.  But without
the "useBean" tag, the "html:options" tag can't find the data it needs.
I've read the docs until my eyes hurt, and I can't find any technique to
tell "useBean" to merely create a new bean, not fetch an existing one.
This seems so easy, I can't believe I haven't done it before, but I can't
find an example in any of my previous code.

What am I missing?     Thanks in advance...

--
Bob Langford
Silicon Masters Consulting, Inc.    8207 Stone River Court, Richmond, VA
23235
phone:  804-674-1253      fax:  804-745-6650 
http://www.silicon-masters.com/  



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to