View Javadoc

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