1 18 19 20 package org.apache.struts.webapp.example; 21 22 23 import java.io.IOException ; 24 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 27 import javax.servlet.jsp.JspException ; 28 import javax.servlet.jsp.JspWriter ; 29 import javax.servlet.jsp.tagext.TagSupport ; 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 42 43 public class LinkUserTag extends TagSupport { 44 45 46 48 49 52 protected String page = null; 53 54 55 58 protected static MessageResources messages = 59 MessageResources.getMessageResources 60 ("org.apache.struts.webapp.example.ApplicationResources"); 61 62 63 66 private String name = "user"; 67 68 69 71 72 75 public String getPage() { 76 77 return (this.page); 78 79 } 80 81 82 87 public void setPage(String page) { 88 89 this.page = page; 90 91 } 92 93 94 97 public String getName() { 98 99 return (this.name); 100 101 } 102 103 104 109 public void setName(String name) { 110 111 this.name = name; 112 113 } 114 115 116 118 119 124 public int doStartTag() throws JspException { 125 126 ModuleConfig config = (ModuleConfig) pageContext.getRequest() 128 .getAttribute(org.apache.struts.Globals.MODULE_KEY); 129 HttpServletRequest request = 130 (HttpServletRequest ) pageContext.getRequest(); 131 StringBuffer url = new StringBuffer (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 e) { 138 user = null; 139 } 140 if (user == null) 141 throw new JspException 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 HttpServletResponse response = 152 (HttpServletResponse ) pageContext.getResponse(); 153 StringBuffer results = new StringBuffer ("<a HREF=\""); 154 results.append(response.encodeURL(url.toString())); 155 results.append("\">"); 156 157 JspWriter writer = pageContext.getOut(); 159 try { 160 writer.print(results.toString()); 161 } catch (IOException e) { 162 throw new JspException 163 (messages.getMessage("linkUser.io", e.toString())); 164 } 165 166 return (EVAL_BODY_INCLUDE); 168 169 } 170 171 172 173 178 public int doEndTag() throws JspException { 179 180 181 JspWriter writer = pageContext.getOut(); 183 try { 184 writer.print("</a>"); 185 } catch (IOException e) { 186 throw new JspException 187 (messages.getMessage("link.io", e.toString())); 188 } 189 190 return (EVAL_PAGE); 191 192 } 193 194 195 198 public void release() { 199 200 super.release(); 201 this.page = null; 202 this.name = "user"; 203 204 } 205 206 207 } 208 | Popular Tags |