KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > html > RandomLinkTag


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.taglib.core.html;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.apache.struts.taglib.TagUtils;
21
22 import javax.servlet.jsp.JspException JavaDoc;
23 import javax.servlet.jsp.PageContext JavaDoc;
24 import javax.servlet.jsp.tagext.SimpleTagSupport JavaDoc;
25
26 /**
27  * <p>Adds query parameter with random value to the specified URI.
28  * This can be useful for links that point to dynamically generated images
29  * (for example, servlet that selects an image by its ID and does some
30  * transformations on it before output), because browsers usually cache image
31  * with specific URL even if headers forbid this. In this case, you may use
32  * this tag to make URI 'unique', so image will not be cached.
33  * </p>
34  * <p>
35  * Allowed attributes are:
36  * <ul>
37  * <li>
38  * <b>value</b> - required - URI that needs to be made 'unique'
39  * </li>
40  * <li>
41  * <b>var</b> - name of variable that will accept result. If not specified,
42  * result will be written to page.
43  * </li>
44  * <li>
45  * <b>scope</b> - scope of variable that will accept result. If <b>var</b> is
46  * not specified, this is ignored.
47  * </li>
48  * </ul>
49  * </p>
50  * <p>
51  * Here's an example:
52  * <pre>
53  * &lt;img SRC="&lt;atleap:randomLink value="${ctxPath}/servlet/thumbnail/" /&gt;" border="0" /&gt;
54  * </pre>
55  * IMG tag will have unique src each time page is displayed.
56  * </p>
57  * <p><a HREF="RandomLinkTag.java.htm"><i>View Source</i></a></p>
58  *
59  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
60  * @version $Revision: 1.8 $ $Date: 2005/10/12 13:35:01 $
61  * @jsp.tag name="randomLink"
62  * body-content="empty"
63  */

64 public class RandomLinkTag extends SimpleTagSupport JavaDoc {
65
66     protected transient final Log log = LogFactory.getLog(RandomLinkTag.class);
67
68     /**
69      * Value to rewrite
70      */

71     protected String JavaDoc value;
72
73     /**
74      * Name of variable to export calculated value
75      */

76     protected String JavaDoc var;
77
78     /**
79      * Scope to export variable to
80      */

81     protected String JavaDoc scope;
82
83     /**
84      * Returns value to rewrite
85      *
86      * @return value to rewrite
87      * @jsp.attribute required="true"
88      * rtexprvalue="true"
89      * type="java.lang.String"
90      * description="Value to rewrite"
91      */

92     public String JavaDoc getValue() {
93         return value;
94     }
95
96     /**
97      * Sets value to rewrite
98      *
99      * @param value value to rewrite to set
100      */

101     public void setValue(String JavaDoc value) {
102         this.value = value;
103     }
104
105     /**
106      * Returns name of variable which will accept the result
107      *
108      * @return name of variable
109      * @jsp.attribute required="false"
110      * rtexprvalue="true"
111      * type="java.lang.String"
112      * description="Name of variable to export message"
113      */

114     public String JavaDoc getVar() {
115         return var;
116     }
117
118     /**
119      * Sets name of variable which will accept the result
120      *
121      * @param var name of variable to set
122      */

123     public void setVar(String JavaDoc var) {
124         this.var = var;
125     }
126
127     /**
128      * Returns variable scope
129      *
130      * @return variable scope
131      * @jsp.attribute required="false"
132      * rtexprvalue="true"
133      * type="java.lang.String"
134      * description="Scope to export variable to"
135      */

136     public String JavaDoc getScope() {
137         return scope;
138     }
139
140     /**
141      * Sets variable scope
142      *
143      * @param scope variable scope to set
144      */

145     public void setScope(String JavaDoc scope) {
146         this.scope = scope;
147     }
148
149     /**
150      * Processes the tag
151      *
152      * @throws JspException
153      */

154     public void doTag() throws JspException JavaDoc {
155
156         PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
157
158         double randomValue = Math.random();
159         String JavaDoc rnd = "rnd=" + randomValue;
160         if ( value.indexOf('?') != -1 ) {
161             value += "&" + rnd;
162         } else {
163             value += "?" + rnd;
164         }
165
166         TagUtils tagUtils = TagUtils.getInstance();
167         if ( var != null ) {
168             // save value in specified scope
169
int varScope = PageContext.PAGE_SCOPE;
170             if ( scope != null ) {
171                 varScope = tagUtils.getScope(scope);
172             }
173             pageContext.setAttribute(var, value, varScope);
174
175         } else {
176             tagUtils.write(pageContext, value);
177         }
178     }
179 }
180
Popular Tags