001: /*
002: * $Id: PrepareOptionsAction.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package examples.options;
023:
024: import java.util.ArrayList;
025: import java.util.HashMap;
026:
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.apache.struts.action.Action;
031: import org.apache.struts.action.ActionForm;
032: import org.apache.struts.action.ActionForward;
033: import org.apache.struts.action.ActionMapping;
034: import org.apache.struts.util.LabelValueBean;
035:
036: /**
037: * Perform any tasks and setup any data that
038: * must be prepared before the form is displayed.
039: *
040: * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
041: */
042: public class PrepareOptionsAction extends Action {
043:
044: // ------------------------------------------------------------ Constructors
045:
046: /**
047: * Constructor for PrepareOptionsAction.
048: */
049: public PrepareOptionsAction() {
050: super ();
051: }
052:
053: // ---------------------------------------------------------- Action Methods
054:
055: /**
056: * Process the request and return an <code>ActionForward</code> instance
057: * describing where and how control should be forwarded, or
058: * <code>null</code>if the response has already been completed.
059: *
060: * @param mapping The ActionMapping used to select this instance
061: * @param form The optional ActionForm bean for this request (if any)
062: * @param request The HTTP request we are processing
063: * @param response The HTTP response we are creating
064: *
065: * @exception Exception if an exception occurs
066: *
067: * @return the ActionForward to forward control to
068: */
069: public ActionForward execute(ActionMapping mapping,
070: ActionForm form, HttpServletRequest request,
071: HttpServletResponse response) throws Exception {
072:
073: /* An array */
074: String[] fruits = { "Strawberry", "Apple", "Orange", "Pear",
075: "Mango", "Banana", "Pineapple" };
076: request.setAttribute("fruits", fruits);
077:
078: /* Two arrays - one for labels and one for values */
079: String[] colors = { "Red", "Orange", "Yellow", "Green", "Blue",
080: "Indigo", "Violet" };
081: request.setAttribute("colors", colors);
082:
083: String[] colorCodes = { "#FF0000", "#FFA500", "#FFFF00",
084: "#00FF00", "#0000FF", "#4B0082", "#EE82EE" };
085: request.setAttribute("colorCodes", colorCodes);
086:
087: /* A Collection */
088: ArrayList colorList = new ArrayList();
089: colorList.add("Red");
090: colorList.add("Orange");
091: colorList.add("Yellow");
092: colorList.add("Green");
093: colorList.add("Blue");
094: colorList.add("Indigo");
095: colorList.add("Violet");
096: request.setAttribute("colorCollection", colorList);
097:
098: /* A Collection of LabelValue beans */
099: ArrayList days = new ArrayList();
100: days.add(new LabelValueBean("Monday", "1"));
101: days.add(new LabelValueBean("Tuesday", "2"));
102: days.add(new LabelValueBean("Wednesday", "3"));
103: days.add(new LabelValueBean("Thursday", "4"));
104: days.add(new LabelValueBean("Friday", "5"));
105: days.add(new LabelValueBean("Saturday", "6"));
106: days.add(new LabelValueBean("Sunday", "7"));
107: request.setAttribute("days", days);
108:
109: /* Collection of custom beans */
110: ArrayList books = new ArrayList();
111: books.add(new BookBean("0596003285",
112: "Programming Jakarta Struts"));
113: books.add(new BookBean("1930110502", "Struts in Action"));
114: books.add(new BookBean("1861007817",
115: "Professional Struts Applications"));
116: books.add(new BookBean("0672324725", "Struts Kick Start"));
117: books
118: .add(new BookBean("0471213020",
119: "Mastering Jakarta Struts"));
120: books.add(new BookBean("1558608621", "The Struts Framework"));
121: books.add(new BookBean("0971661901", "Struts Fast Track"));
122: request.setAttribute("books", books);
123:
124: /* A Map
125: *
126: * Note: We are using a HashMap which is unsorted - the resulting
127: * options could appear in any order. If you want to your options to be
128: * in a particular order you should you a SortedMap implementation such
129: * as the TreeMap. This behaviour is a feature of the standard Map
130: * implementaions and nothing to to with Struts tags.
131: *
132: * Also, we've used an Integer as the key to demonstrate that the
133: * <html:options> and <html:optionsCollection> tags do not require
134: * String values for the label and values. If they are passed an object
135: * other than a String, they will automatically call the toString()
136: * method and use the result.
137: */
138: HashMap animals = new HashMap();
139: animals.put(new Integer(1), "Cat");
140: animals.put(new Integer(2), "Dog");
141: animals.put(new Integer(3), "Horse");
142: animals.put(new Integer(4), "Rabbit");
143: animals.put(new Integer(5), "Goldfish");
144: request.setAttribute("animals", animals);
145:
146: // Forward to form page
147: return mapping.findForward("success");
148:
149: }
150:
151: }
|