2
* Copyright 1999-2001,2004 The Apache Software Foundation.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
package org.apache.struts.webapp.example;
21
import java.io.IOException;
23
import javax.servlet.http.HttpServletRequest;
24
import javax.servlet.http.HttpServletResponse;
25
import javax.servlet.jsp.JspException;
26
import javax.servlet.jsp.JspWriter;
27
import javax.servlet.jsp.tagext.TagSupport;
29
import org.apache.struts.util.MessageResources;
30
import org.apache.struts.util.ResponseUtils;
34
* Generate a URL-encoded hyperlink to the specified URI, with
35
* associated query parameters selecting a specified User.
37
* @author Craig R. McClanahan
38
* @version $Revision: 1.4 $ $Date: 2004/03/08 02:49:52 $
41
public class LinkUserTag extends TagSupport {
44
// ------------------------------------------------------ Instance Variables
50
protected String page = null;
54
* The message resources for this package.
56
protected static MessageResources messages =
57
MessageResources.getMessageResources
58
("org.apache.struts.webapp.example.ApplicationResources");
64
private String name = "user";
67
// ------------------------------------------------------------- Properties
71
* Return the hyperlink URI.
73
public String getPage() {
81
* Set the hyperlink URI.
83
* @param page Set the hyperlink URI
85
public void setPage(String page) {
93
* Return the attribute name.
95
public String getName() {
103
* Set the attribute name.
105
* @param name The new attribute name
107
public void setName(String name) {
114
// --------------------------------------------------------- Public Methods
118
* Render the beginning of the hyperlink.
120
* @exception JspException if a JSP exception has occurred
122
public int doStartTag() throws JspException {
124
// Generate the URL to be encoded
125
HttpServletRequest request =
126
(HttpServletRequest) pageContext.getRequest();
127
StringBuffer url = new StringBuffer(request.getContextPath());
131
user = (User) pageContext.findAttribute(name);
132
} catch (ClassCastException e) {
136
throw new JspException
137
(messages.getMessage("linkUser.noUser", name));
138
if (page.indexOf("?") < 0)
142
url.append("username=");
143
url.append(ResponseUtils.filter(user.getUsername()));
145
// Generate the hyperlink start element
146
HttpServletResponse response =
147
(HttpServletResponse) pageContext.getResponse();
148
StringBuffer results = new StringBuffer("<a href=\"");
149
results.append(response.encodeURL(url.toString()));
150
results.append("\">");
152
// Print this element to our output writer
153
JspWriter writer = pageContext.getOut();
155
writer.print(results.toString());
156
} catch (IOException e) {
157
throw new JspException
158
(messages.getMessage("linkUser.io", e.toString()));
161
// Evaluate the body of this tag
162
return (EVAL_BODY_INCLUDE);
169
* Render the end of the hyperlink.
171
* @exception JspException if a JSP exception has occurred
173
public int doEndTag() throws JspException {
176
// Print the ending element to our output writer
177
JspWriter writer = pageContext.getOut();
179
writer.print("</a>");
180
} catch (IOException e) {
181
throw new JspException
182
(messages.getMessage("link.io", e.toString()));
191
* Release any acquired resources.
193
public void release() {