View Javadoc

1   /*
2    * $Id: ParameterTag.java 376840 2006-02-10 21:00:51Z husted $
3    *
4    * Copyright 1999-2004 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  package org.apache.struts.taglib.bean;
19  
20  import org.apache.struts.taglib.TagUtils;
21  import org.apache.struts.util.MessageResources;
22  
23  import javax.servlet.jsp.JspException;
24  import javax.servlet.jsp.tagext.TagSupport;
25  
26  /***
27   * Define a scripting variable based on the value(s) of the specified
28   * parameter received with this request.
29   *
30   * @version $Rev: 376840 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
31   *          $
32   */
33  public class ParameterTag extends TagSupport {
34      /***
35       * The message resources for this package.
36       */
37      protected static MessageResources messages =
38          MessageResources.getMessageResources(
39              "org.apache.struts.taglib.bean.LocalStrings");
40  
41      // ------------------------------------------------------------- Properties
42  
43      /***
44       * The name of the scripting variable that will be exposed as a page scope
45       * attribute.
46       */
47      protected String id = null;
48  
49      /***
50       * Return an array of parameter values if <code>multiple</code> is
51       * non-null.
52       */
53      protected String multiple = null;
54  
55      /***
56       * The name of the parameter whose value is to be exposed.
57       */
58      protected String name = null;
59  
60      /***
61       * The default value to return if no parameter of the specified name is
62       * found.
63       */
64      protected String value = null;
65  
66      public String getId() {
67          return (this.id);
68      }
69  
70      public void setId(String id) {
71          this.id = id;
72      }
73  
74      public String getMultiple() {
75          return (this.multiple);
76      }
77  
78      public void setMultiple(String multiple) {
79          this.multiple = multiple;
80      }
81  
82      public String getName() {
83          return (this.name);
84      }
85  
86      public void setName(String name) {
87          this.name = name;
88      }
89  
90      public String getValue() {
91          return (this.value);
92      }
93  
94      public void setValue(String value) {
95          this.value = value;
96      }
97  
98      // --------------------------------------------------------- Public Methods
99  
100     /***
101      * Retrieve the required property and expose it as a scripting variable.
102      *
103      * @throws JspException if a JSP exception has occurred
104      */
105     public int doStartTag() throws JspException {
106         // Deal with a single parameter value
107         if (multiple == null) {
108             String value = pageContext.getRequest().getParameter(name);
109 
110             if ((value == null) && (this.value != null)) {
111                 value = this.value;
112             }
113 
114             if (value == null) {
115                 JspException e =
116                     new JspException(messages.getMessage("parameter.get", name));
117 
118                 TagUtils.getInstance().saveException(pageContext, e);
119                 throw e;
120             }
121 
122             pageContext.setAttribute(id, value);
123 
124             return (SKIP_BODY);
125         }
126 
127         // Deal with multiple parameter values
128         String[] values = pageContext.getRequest().getParameterValues(name);
129 
130         if ((values == null) || (values.length == 0)) {
131             if (this.value != null) {
132                 values = new String[] { this.value };
133             }
134         }
135 
136         if ((values == null) || (values.length == 0)) {
137             JspException e =
138                 new JspException(messages.getMessage("parameter.get", name));
139 
140             TagUtils.getInstance().saveException(pageContext, e);
141             throw e;
142         }
143 
144         pageContext.setAttribute(id, values);
145 
146         return (SKIP_BODY);
147     }
148 
149     /***
150      * Release all allocated resources.
151      */
152     public void release() {
153         super.release();
154         id = null;
155         multiple = null;
156         name = null;
157         value = null;
158     }
159 }