1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
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
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
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 }