KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > webapp > example > LinkUserTag


1 /*
2  * $Id: LinkUserTag.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
20 package org.apache.struts.webapp.example;
21
22
23 import java.io.IOException JavaDoc;
24
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27 import javax.servlet.jsp.JspException JavaDoc;
28 import javax.servlet.jsp.JspWriter JavaDoc;
29 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
30
31 import org.apache.struts.config.ModuleConfig;
32 import org.apache.struts.taglib.TagUtils;
33 import org.apache.struts.util.MessageResources;
34
35
36 /**
37  * Generate a URL-encoded hyperlink to the specified URI, with
38  * associated query parameters selecting a specified User.
39  *
40  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
41  */

42
43 public class LinkUserTag extends TagSupport JavaDoc {
44
45
46     // ----------------------------------------------------- Instance Variables
47

48
49     /**
50      * The hyperlink URI.
51      */

52     protected String JavaDoc page = null;
53
54
55     /**
56      * The message resources for this package.
57      */

58     protected static MessageResources messages =
59     MessageResources.getMessageResources
60     ("org.apache.struts.webapp.example.ApplicationResources");
61
62
63     /**
64      * The attribute name.
65      */

66     private String JavaDoc name = "user";
67
68
69     // ------------------------------------------------------------- Properties
70

71
72     /**
73      * Return the hyperlink URI.
74      */

75     public String JavaDoc getPage() {
76
77     return (this.page);
78
79     }
80
81
82     /**
83      * Set the hyperlink URI.
84      *
85      * @param page Set the hyperlink URI
86      */

87     public void setPage(String JavaDoc page) {
88
89     this.page = page;
90
91     }
92
93
94     /**
95      * Return the attribute name.
96      */

97     public String JavaDoc getName() {
98
99     return (this.name);
100
101     }
102
103
104     /**
105      * Set the attribute name.
106      *
107      * @param name The new attribute name
108      */

109     public void setName(String JavaDoc name) {
110
111     this.name = name;
112
113     }
114
115
116     // --------------------------------------------------------- Public Methods
117

118
119     /**
120      * Render the beginning of the hyperlink.
121      *
122      * @exception JspException if a JSP exception has occurred
123      */

124     public int doStartTag() throws JspException JavaDoc {
125
126     // Generate the URL to be encoded
127
ModuleConfig config = (ModuleConfig) pageContext.getRequest()
128             .getAttribute(org.apache.struts.Globals.MODULE_KEY);
129         HttpServletRequest JavaDoc request =
130             (HttpServletRequest JavaDoc) pageContext.getRequest();
131         StringBuffer JavaDoc url = new StringBuffer JavaDoc(request.getContextPath());
132     url.append(config.getPrefix());
133         url.append(page);
134     User user = null;
135     try {
136         user = (User) pageContext.findAttribute(name);
137         } catch (ClassCastException JavaDoc e) {
138         user = null;
139     }
140     if (user == null)
141         throw new JspException JavaDoc
142             (messages.getMessage("linkUser.noUser", name));
143     if (page.indexOf("?") < 0)
144         url.append("?");
145     else
146         url.append("&");
147     url.append("username=");
148     url.append(TagUtils.getInstance().filter(user.getUsername()));
149
150     // Generate the hyperlink start element
151
HttpServletResponse JavaDoc response =
152       (HttpServletResponse JavaDoc) pageContext.getResponse();
153     StringBuffer JavaDoc results = new StringBuffer JavaDoc("<a HREF=\"");
154     results.append(response.encodeURL(url.toString()));
155     results.append("\">");
156
157     // Print this element to our output writer
158
JspWriter JavaDoc writer = pageContext.getOut();
159     try {
160         writer.print(results.toString());
161     } catch (IOException JavaDoc e) {
162         throw new JspException JavaDoc
163         (messages.getMessage("linkUser.io", e.toString()));
164     }
165
166     // Evaluate the body of this tag
167
return (EVAL_BODY_INCLUDE);
168
169     }
170
171
172
173     /**
174      * Render the end of the hyperlink.
175      *
176      * @exception JspException if a JSP exception has occurred
177      */

178     public int doEndTag() throws JspException JavaDoc {
179
180
181     // Print the ending element to our output writer
182
JspWriter JavaDoc writer = pageContext.getOut();
183     try {
184         writer.print("</a>");
185     } catch (IOException JavaDoc e) {
186         throw new JspException JavaDoc
187             (messages.getMessage("link.io", e.toString()));
188     }
189
190     return (EVAL_PAGE);
191
192     }
193
194
195     /**
196      * Release any acquired resources.
197      */

198     public void release() {
199
200         super.release();
201         this.page = null;
202         this.name = "user";
203
204     }
205
206
207 }
208
Popular Tags