1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.apache.struts.webapp.example2;
24
25
26 import java.io.IOException;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import javax.servlet.jsp.JspException;
30 import javax.servlet.jsp.JspWriter;
31 import javax.servlet.jsp.tagext.TagSupport;
32 import org.apache.struts.util.MessageResources;
33 import org.apache.struts.util.ResponseUtils;
34
35
36 /**
37 * Generate a URL-encoded hyperlink to the specified URI, with
38 * associated query parameters selecting a specified User.
39 *
40 * @author Craig R. McClanahan
41 * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
42 */
43
44 public class LinkUserTag extends TagSupport {
45
46
47
48
49
50 /**
51 * The hyperlink URI.
52 */
53 protected String page = null;
54
55
56 /**
57 * The message resources for this package.
58 */
59 protected static MessageResources messages =
60 MessageResources.getMessageResources
61 ("org.apache.struts.webapp.example.ApplicationResources");
62
63
64 /**
65 * The attribute name.
66 */
67 private String name = "user";
68
69
70
71
72
73 /**
74 * Return the hyperlink URI.
75 */
76 public String getPage() {
77
78 return (this.page);
79
80 }
81
82
83 /**
84 * Set the hyperlink URI.
85 *
86 * @param page Set the hyperlink URI
87 */
88 public void setPage(String page) {
89
90 this.page = page;
91
92 }
93
94
95 /**
96 * Return the attribute name.
97 */
98 public String getName() {
99
100 return (this.name);
101
102 }
103
104
105 /**
106 * Set the attribute name.
107 *
108 * @param name The new attribute name
109 */
110 public void setName(String name) {
111
112 this.name = name;
113
114 }
115
116
117
118
119
120 /**
121 * Render the beginning of the hyperlink.
122 *
123 * @exception JspException if a JSP exception has occurred
124 */
125 public int doStartTag() throws JspException {
126
127
128 HttpServletRequest request =
129 (HttpServletRequest) pageContext.getRequest();
130 StringBuffer url = new StringBuffer(request.getContextPath());
131 url.append(page);
132 User user = null;
133 try {
134 user = (User) pageContext.findAttribute(name);
135 } catch (ClassCastException e) {
136 user = null;
137 }
138 if (user == null)
139 throw new JspException
140 (messages.getMessage("linkUser.noUser", name));
141 if (page.indexOf("?") < 0)
142 url.append("?");
143 else
144 url.append("&");
145 url.append("username=");
146 url.append(ResponseUtils.filter(user.getUsername()));
147
148
149 HttpServletResponse response =
150 (HttpServletResponse) pageContext.getResponse();
151 StringBuffer results = new StringBuffer("<a href=\"");
152 results.append(response.encodeURL(url.toString()));
153 results.append("\">");
154
155
156 JspWriter writer = pageContext.getOut();
157 try {
158 writer.print(results.toString());
159 } catch (IOException e) {
160 throw new JspException
161 (messages.getMessage("linkUser.io", e.toString()));
162 }
163
164
165 return (EVAL_BODY_INCLUDE);
166
167 }
168
169
170
171 /**
172 * Render the end of the hyperlink.
173 *
174 * @exception JspException if a JSP exception has occurred
175 */
176 public int doEndTag() throws JspException {
177
178
179
180 JspWriter writer = pageContext.getOut();
181 try {
182 writer.print("</a>");
183 } catch (IOException e) {
184 throw new JspException
185 (messages.getMessage("link.io", e.toString()));
186 }
187
188 return (EVAL_PAGE);
189
190 }
191
192
193 /**
194 * Release any acquired resources.
195 */
196 public void release() {
197
198 super.release();
199 this.page = null;
200 this.name = "user";
201
202 }
203
204
205 }