1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts.taglib.html;
19
20 import org.apache.struts.taglib.TagUtils;
21 import org.apache.struts.util.MessageResources;
22
23 import javax.servlet.jsp.JspException;
24
25 import java.net.MalformedURLException;
26
27 import java.util.HashMap;
28 import java.util.Map;
29
30 /***
31 * Generate a URL-encoded hyperlink to the specified URI.
32 *
33 * @version $Rev: 376841 $ $Date: 2005-04-06 02:37:00 -0400 (Wed, 06 Apr 2005)
34 * $
35 */
36 public class LinkTag extends BaseHandlerTag {
37 /***
38 * The message resources for this package.
39 */
40 protected static MessageResources messages =
41 MessageResources.getMessageResources(Constants.Package
42 + ".LocalStrings");
43
44
45
46 /***
47 * The body content of this tag (if any).
48 */
49 protected String text = null;
50
51
52
53 /***
54 * The anchor to be added to the end of the generated hyperlink.
55 */
56 protected String anchor = null;
57
58 /***
59 * <p>The logical forward name from which to retrieve the hyperlink
60 * URI.</p> <p>Usage note: If a forward config is used in a hyperlink, and
61 * a module is specified, the path must lead to another action and not
62 * directly to a page. This is in keeping with rule that in a modular
63 * application all links must be to an action rather than a page. </p>
64 */
65 protected String forward = null;
66
67 /***
68 * The hyperlink URI.
69 */
70 protected String href = null;
71
72 /***
73 * The link name for named links.
74 */
75 protected String linkName = null;
76
77 /***
78 * The JSP bean name for query parameters.
79 */
80 protected String name = null;
81
82 /***
83 * The module-relative page URL (beginning with a slash) to which this
84 * hyperlink will be rendered.
85 */
86 protected String page = null;
87
88 /***
89 * The module-relative action (beginning with a slash) which will be
90 * called by this link
91 */
92 protected String action = null;
93
94 /***
95 * The module prefix (beginning with a slash) which will be used to find
96 * the action for this link.
97 */
98 protected String module = null;
99
100 /***
101 * The single-parameter request parameter name to generate.
102 */
103 protected String paramId = null;
104
105 /***
106 * The single-parameter JSP bean name.
107 */
108 protected String paramName = null;
109
110 /***
111 * The single-parameter JSP bean property.
112 */
113 protected String paramProperty = null;
114
115 /***
116 * The single-parameter JSP bean scope.
117 */
118 protected String paramScope = null;
119
120 /***
121 * The JSP bean property name for query parameters.
122 */
123 protected String property = null;
124
125 /***
126 * The scope of the bean specified by the name property, if any.
127 */
128 protected String scope = null;
129
130 /***
131 * The window target.
132 */
133 protected String target = null;
134
135 /***
136 * Include transaction token (if any) in the hyperlink?
137 */
138 protected boolean transaction = false;
139
140 /***
141 * Name of parameter to generate to hold index number
142 */
143 protected String indexId = null;
144 protected boolean useLocalEncoding = false;
145
146
147 public LinkTag() {
148 super();
149 doDisabled = false;
150 }
151
152 public String getAnchor() {
153 return (this.anchor);
154 }
155
156 public void setAnchor(String anchor) {
157 this.anchor = anchor;
158 }
159
160 public String getForward() {
161 return (this.forward);
162 }
163
164 public void setForward(String forward) {
165 this.forward = forward;
166 }
167
168 public String getHref() {
169 return (this.href);
170 }
171
172 public void setHref(String href) {
173 this.href = href;
174 }
175
176 public String getLinkName() {
177 return (this.linkName);
178 }
179
180 public void setLinkName(String linkName) {
181 this.linkName = linkName;
182 }
183
184 public String getName() {
185 return (this.name);
186 }
187
188 public void setName(String name) {
189 this.name = name;
190 }
191
192 public String getPage() {
193 return (this.page);
194 }
195
196 public void setPage(String page) {
197 this.page = page;
198 }
199
200 public String getAction() {
201 return (this.action);
202 }
203
204 public void setAction(String action) {
205 this.action = action;
206 }
207
208 public String getModule() {
209 return (this.module);
210 }
211
212 public void setModule(String module) {
213 this.module = module;
214 }
215
216 public String getParamId() {
217 return (this.paramId);
218 }
219
220 public void setParamId(String paramId) {
221 this.paramId = paramId;
222 }
223
224 public String getParamName() {
225 return (this.paramName);
226 }
227
228 public void setParamName(String paramName) {
229 this.paramName = paramName;
230 }
231
232 public String getParamProperty() {
233 return (this.paramProperty);
234 }
235
236 public void setParamProperty(String paramProperty) {
237 this.paramProperty = paramProperty;
238 }
239
240 public String getParamScope() {
241 return (this.paramScope);
242 }
243
244 public void setParamScope(String paramScope) {
245 this.paramScope = paramScope;
246 }
247
248 public String getProperty() {
249 return (this.property);
250 }
251
252 public void setProperty(String property) {
253 this.property = property;
254 }
255
256 public String getScope() {
257 return (this.scope);
258 }
259
260 public void setScope(String scope) {
261 this.scope = scope;
262 }
263
264 public String getTarget() {
265 return (this.target);
266 }
267
268 public void setTarget(String target) {
269 this.target = target;
270 }
271
272 public boolean getTransaction() {
273 return (this.transaction);
274 }
275
276 public void setTransaction(boolean transaction) {
277 this.transaction = transaction;
278 }
279
280 public String getIndexId() {
281 return (this.indexId);
282 }
283
284 public void setIndexId(String indexId) {
285 this.indexId = indexId;
286 }
287
288 public boolean isUseLocalEncoding() {
289 return useLocalEncoding;
290 }
291
292 public void setUseLocalEncoding(boolean b) {
293 useLocalEncoding = b;
294 }
295
296
297
298 /***
299 * Render the beginning of the hyperlink. <p> Support for indexed property
300 * since Struts 1.1
301 *
302 * @throws JspException if a JSP exception has occurred
303 */
304 public int doStartTag() throws JspException {
305
306 StringBuffer results = new StringBuffer("<a");
307
308
309 prepareAttribute(results, "name", getLinkName());
310
311
312 if ((getLinkName() == null) || (getForward() != null)
313 || (getHref() != null) || (getPage() != null)
314 || (getAction() != null)) {
315 prepareAttribute(results, "href", calculateURL());
316 }
317
318 prepareAttribute(results, "target", getTarget());
319 prepareAttribute(results, "accesskey", getAccesskey());
320 prepareAttribute(results, "tabindex", getTabindex());
321 results.append(prepareStyles());
322 results.append(prepareEventHandlers());
323 prepareOtherAttributes(results);
324 results.append(">");
325
326 TagUtils.getInstance().write(pageContext, results.toString());
327
328
329 this.text = null;
330
331 return (EVAL_BODY_TAG);
332 }
333
334 /***
335 * Save the associated label from the body content.
336 *
337 * @throws JspException if a JSP exception has occurred
338 */
339 public int doAfterBody() throws JspException {
340 if (bodyContent != null) {
341 String value = bodyContent.getString().trim();
342
343 if (value.length() > 0) {
344 text = value;
345 }
346 }
347
348 return (SKIP_BODY);
349 }
350
351 /***
352 * Render the end of the hyperlink.
353 *
354 * @throws JspException if a JSP exception has occurred
355 */
356 public int doEndTag() throws JspException {
357
358 StringBuffer results = new StringBuffer();
359
360 if (text != null) {
361 results.append(text);
362 }
363
364 results.append("</a>");
365
366 TagUtils.getInstance().write(pageContext, results.toString());
367
368 return (EVAL_PAGE);
369 }
370
371 /***
372 * Release any acquired resources.
373 */
374 public void release() {
375 super.release();
376 anchor = null;
377 forward = null;
378 href = null;
379 linkName = null;
380 name = null;
381 page = null;
382 action = null;
383 module = null;
384 paramId = null;
385 paramName = null;
386 paramProperty = null;
387 paramScope = null;
388 property = null;
389 scope = null;
390 target = null;
391 text = null;
392 transaction = false;
393 indexId = null;
394 useLocalEncoding = false;
395 }
396
397
398
399 /***
400 * Return the complete URL to which this hyperlink will direct the user.
401 * Support for indexed property since Struts 1.1
402 *
403 * @throws JspException if an exception is thrown calculating the value
404 */
405 protected String calculateURL()
406 throws JspException {
407
408 Map params =
409 TagUtils.getInstance().computeParameters(pageContext, paramId,
410 paramName, paramProperty, paramScope, name, property, scope,
411 transaction);
412
413
414
415 if (indexed) {
416 int indexValue = getIndexValue();
417
418
419 if (params == null) {
420 params = new HashMap();
421 }
422
423 if (indexId != null) {
424 params.put(indexId, Integer.toString(indexValue));
425 } else {
426 params.put("index", Integer.toString(indexValue));
427 }
428 }
429
430 String url = null;
431
432 try {
433 url = TagUtils.getInstance().computeURLWithCharEncoding(pageContext,
434 forward, href, page, action, module, params, anchor, false,
435 useLocalEncoding);
436 } catch (MalformedURLException e) {
437 TagUtils.getInstance().saveException(pageContext, e);
438 throw new JspException(messages.getMessage("rewrite.url",
439 e.toString()));
440 }
441
442 return (url);
443 }
444 }