View Javadoc
1   /*
2    * $Id$
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package examples.options;
23  
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  
27  import jakarta.servlet.http.HttpServletRequest;
28  import jakarta.servlet.http.HttpServletResponse;
29  
30  import org.apache.struts.action.Action;
31  import org.apache.struts.action.ActionForm;
32  import org.apache.struts.action.ActionForward;
33  import org.apache.struts.action.ActionMapping;
34  import org.apache.struts.util.LabelValueBean;
35  
36  /**
37   * Perform any tasks and setup any data that
38   * must be prepared before the form is displayed.
39   *
40   * @version $Rev$ $Date$
41   */
42  public class PrepareOptionsAction extends Action {
43      private static final long serialVersionUID = -138527557363506974L;
44  
45      // ------------------------------------------------------------ Constructors
46  
47      /**
48       * Constructor for PrepareOptionsAction.
49       */
50      public PrepareOptionsAction() {
51          super();
52      }
53  
54      // ---------------------------------------------------------- Action Methods
55  
56      /**
57       * Process the request and return an <code>ActionForward</code> instance
58       * describing where and how control should be forwarded, or
59       * <code>null</code>if the response has already been completed.
60       *
61       * @param mapping The ActionMapping used to select this instance
62       * @param form The optional ActionForm bean for this request (if any)
63       * @param request The HTTP request we are processing
64       * @param response The HTTP response we are creating
65       *
66       * @exception Exception if an exception occurs
67       *
68       * @return the ActionForward to forward control to
69       */
70      public ActionForward execute(
71          ActionMapping mapping,
72          ActionForm form,
73          HttpServletRequest request,
74          HttpServletResponse response)
75          throws Exception {
76  
77          /* An array */
78          String[] fruits =
79              {
80                  "Strawberry",
81                  "Apple",
82                  "Orange",
83                  "Pear",
84                  "Mango",
85                  "Banana",
86                  "Pineapple" };
87          request.setAttribute("fruits", fruits);
88  
89          /* Two arrays - one for labels and one for values */
90          String[] colors =
91              { "Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet" };
92          request.setAttribute("colors", colors);
93  
94          String[] colorCodes =
95              {
96                  "#FF0000",
97                  "#FFA500",
98                  "#FFFF00",
99                  "#00FF00",
100                 "#0000FF",
101                 "#4B0082",
102                 "#EE82EE" };
103         request.setAttribute("colorCodes", colorCodes);
104 
105         /* A Collection */
106         ArrayList<String> colorList = new ArrayList<>();
107         colorList.add("Red");
108         colorList.add("Orange");
109         colorList.add("Yellow");
110         colorList.add("Green");
111         colorList.add("Blue");
112         colorList.add("Indigo");
113         colorList.add("Violet");
114         request.setAttribute("colorCollection", colorList);
115 
116         /* A Collection of LabelValue beans */
117         ArrayList<LabelValueBean> days = new ArrayList<>();
118         days.add(new LabelValueBean("Monday", "1"));
119         days.add(new LabelValueBean("Tuesday", "2"));
120         days.add(new LabelValueBean("Wednesday", "3"));
121         days.add(new LabelValueBean("Thursday", "4"));
122         days.add(new LabelValueBean("Friday", "5"));
123         days.add(new LabelValueBean("Saturday", "6"));
124         days.add(new LabelValueBean("Sunday", "7"));
125         request.setAttribute("days", days);
126 
127         /* Collection of custom beans */
128         ArrayList<BookBean> books = new ArrayList<>();
129         books.add(new BookBean("0596003285", "Programming Jakarta Struts"));
130         books.add(new BookBean("1930110502", "Struts in Action"));
131         books.add(
132             new BookBean("1861007817", "Professional Struts Applications"));
133         books.add(new BookBean("0672324725", "Struts Kick Start"));
134         books.add(new BookBean("0471213020", "Mastering Jakarta Struts"));
135         books.add(new BookBean("1558608621", "The Struts Framework"));
136         books.add(new BookBean("0971661901", "Struts Fast Track"));
137         request.setAttribute("books", books);
138 
139         /* A Map
140          *
141          * Note: We are using a HashMap which is unsorted - the resulting
142          * options could appear in any order. If you want to your options to be
143          * in a particular order you should you a SortedMap implementation such
144          * as the TreeMap. This behaviour is a feature of the standard Map
145          * implementaions and nothing to to with Struts tags.
146          *
147          * Also, we've used an Integer as the key to demonstrate that the
148          * <html:options> and <html:optionsCollection> tags do not require
149          * String values for the label and values. If they are passed an object
150          * other than a String, they will automatically call the toString()
151          * method and use the result.
152          */
153         HashMap<Integer, String> animals = new HashMap<>();
154         animals.put(1, "Cat");
155         animals.put(2, "Dog");
156         animals.put(3, "Horse");
157         animals.put(4, "Rabbit");
158         animals.put(5, "Goldfish");
159         request.setAttribute("animals", animals);
160 
161         // Forward to form page
162         return mapping.findForward("success");
163     }
164 }