KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 1999-2001,2004 The Apache Software Foundation.
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
17
18 package org.apache.struts.webapp.example2;
19
20
21 import java.io.IOException JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.JspWriter JavaDoc;
26 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
27 import org.apache.struts.util.MessageResources;
28 import org.apache.struts.util.ResponseUtils;
29
30
31 /**
32  * Generate a URL-encoded hyperlink to the specified URI, with
33  * associated query parameters selecting a specified User.
34  *
35  * @author Craig R. McClanahan
36  * @version $Rev: 54934 $ $Date: 2004-10-16 18:07:50 +0100 (Sat, 16 Oct 2004) $
37  */

38
39 public class LinkUserTag extends TagSupport JavaDoc {
40
41
42     // ------------------------------------------------------ Instance Variables
43

44
45     /**
46      * The hyperlink URI.
47      */

48     protected String JavaDoc page = null;
49
50
51     /**
52      * The message resources for this package.
53      */

54     protected static MessageResources messages =
55     MessageResources.getMessageResources
56     ("org.apache.struts.webapp.example.ApplicationResources");
57
58
59     /**
60      * The attribute name.
61      */

62     private String JavaDoc name = "user";
63
64
65     // ------------------------------------------------------------- Properties
66

67
68     /**
69      * Return the hyperlink URI.
70      */

71     public String JavaDoc getPage() {
72
73     return (this.page);
74
75     }
76
77
78     /**
79      * Set the hyperlink URI.
80      *
81      * @param page Set the hyperlink URI
82      */

83     public void setPage(String JavaDoc page) {
84
85     this.page = page;
86
87     }
88
89
90     /**
91      * Return the attribute name.
92      */

93     public String JavaDoc getName() {
94
95     return (this.name);
96
97     }
98
99
100     /**
101      * Set the attribute name.
102      *
103      * @param name The new attribute name
104      */

105     public void setName(String JavaDoc name) {
106
107     this.name = name;
108
109     }
110
111
112     // --------------------------------------------------------- Public Methods
113

114
115     /**
116      * Render the beginning of the hyperlink.
117      *
118      * @exception JspException if a JSP exception has occurred
119      */

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

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

191     public void release() {
192
193         super.release();
194         this.page = null;
195         this.name = "user";
196
197     }
198
199
200 }
201
Popular Tags