Wong Kok Wai wrote:

> What's the recommended usage of form:options? The
> problem I see from the example source is I need to
> ensure the list of values need to be synchronised in
> both the JSP and the action form instance.
>

One approach that works well when the list of available options is static would
be a strategy like this:
* In the init() method of a startup servlet, load up two
  arrays of the labels and values for a particular combo box.

* Store the two lists as application scope attributes under
  "well known" bean names.  (Struts uses string constants
  defined in the Action class to make sure that all Java components
  are using the same keys).

    public static final String FOO_LABELS = "foo.labels";
    public static final String FOO_VALUES = "foo.values";

    String labels[] = ...
    String values[] = ...

    getServletContext().setAttribute(MyClass.FOO_LABELS, labels);
    getServletContext().setAttribute(MyClass.FOO_VALUES, values);

* In your ActionForm instances (Struts 1.0 version) you can
  get access to these application scope beans like this:

    String values[] = (String[])
      servlet.getServletContext().getAttribute(MyClass.FOO_VALUES);

* In your JSP pages, you can reference them like this:

    <form:select ...>
        <form:options name="foo.values" labelName="foo.labels"/>
    </form:select>

Craig McClanahan

Reply via email to