KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > bean > ParameterTag


1 /*
2  * $Id: ParameterTag.java 54929 2004-10-16 16:38:42Z germuska $
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
19 package org.apache.struts.taglib.bean;
20
21 import javax.servlet.jsp.JspException JavaDoc;
22 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
23
24 import org.apache.struts.util.MessageResources;
25 import org.apache.struts.taglib.TagUtils;
26
27 /**
28  * Define a scripting variable based on the value(s) of the specified
29  * parameter received with this request.
30  *
31  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
32  */

33 public class ParameterTag extends TagSupport JavaDoc {
34
35     // ------------------------------------------------------------- Properties
36

37     /**
38      * The name of the scripting variable that will be exposed as a page
39      * scope attribute.
40      */

41     protected String JavaDoc id = null;
42
43     public String JavaDoc getId() {
44         return (this.id);
45     }
46
47     public void setId(String JavaDoc id) {
48         this.id = id;
49     }
50
51     /**
52      * The message resources for this package.
53      */

54     protected static MessageResources messages =
55         MessageResources.getMessageResources(
56             "org.apache.struts.taglib.bean.LocalStrings");
57
58     /**
59      * Return an array of parameter values if <code>multiple</code> is
60      * non-null.
61      */

62     protected String JavaDoc multiple = null;
63
64     public String JavaDoc getMultiple() {
65         return (this.multiple);
66     }
67
68     public void setMultiple(String JavaDoc multiple) {
69         this.multiple = multiple;
70     }
71
72     /**
73      * The name of the parameter whose value is to be exposed.
74      */

75     protected String JavaDoc name = null;
76
77     public String JavaDoc getName() {
78         return (this.name);
79     }
80
81     public void setName(String JavaDoc name) {
82         this.name = name;
83     }
84
85     /**
86      * The default value to return if no parameter of the specified name is
87      * found.
88      */

89     protected String JavaDoc value = null;
90
91     public String JavaDoc getValue() {
92         return (this.value);
93     }
94
95     public void setValue(String JavaDoc value) {
96         this.value = value;
97     }
98
99     // --------------------------------------------------------- Public Methods
100

101     /**
102      * Retrieve the required property and expose it as a scripting variable.
103      *
104      * @exception JspException if a JSP exception has occurred
105      */

106     public int doStartTag() throws JspException JavaDoc {
107
108         // Deal with a single parameter value
109
if (multiple == null) {
110             String JavaDoc value = pageContext.getRequest().getParameter(name);
111             if ((value == null) && (this.value != null)) {
112                 value = this.value;
113             }
114
115             if (value == null) {
116                 JspException JavaDoc e =
117                     new JspException JavaDoc(messages.getMessage("parameter.get", name));
118                 TagUtils.getInstance().saveException(pageContext, e);
119                 throw e;
120             }
121
122             pageContext.setAttribute(id, value);
123             return (SKIP_BODY);
124         }
125
126         // Deal with multiple parameter values
127
String JavaDoc values[] = pageContext.getRequest().getParameterValues(name);
128         if ((values == null) || (values.length == 0)) {
129             if (this.value != null) {
130                 values = new String JavaDoc[] { this.value };
131             }
132         }
133
134         if ((values == null) || (values.length == 0)) {
135             JspException JavaDoc e =
136                 new JspException JavaDoc(messages.getMessage("parameter.get", name));
137             TagUtils.getInstance().saveException(pageContext, e);
138             throw e;
139         }
140         
141         pageContext.setAttribute(id, values);
142         return (SKIP_BODY);
143
144     }
145
146     /**
147      * Release all allocated resources.
148      */

149     public void release() {
150
151         super.release();
152         id = null;
153         multiple = null;
154         name = null;
155         value = null;
156
157     }
158
159 }
160
Popular Tags