1 18 19 package org.apache.struts.taglib.html; 20 21 import java.util.Iterator ; 22 23 import javax.servlet.jsp.JspException ; 24 import javax.servlet.jsp.tagext.BodyTagSupport ; 25 26 import org.apache.struts.Globals; 27 import org.apache.struts.action.ActionMessage; 28 import org.apache.struts.action.ActionMessages; 29 import org.apache.struts.taglib.TagUtils; 30 import org.apache.struts.util.MessageResources; 31 32 42 public class MessagesTag extends BodyTagSupport { 43 44 47 protected static MessageResources messageResources = 48 MessageResources.getMessageResources(Constants.Package + ".LocalStrings"); 49 50 54 protected Iterator iterator = null; 55 56 59 protected boolean processed = false; 60 61 64 protected String id = null; 65 66 69 protected String bundle = null; 70 71 74 protected String locale = Globals.LOCALE_KEY; 75 76 79 protected String name = Globals.ERROR_KEY; 80 81 85 protected String property = null; 86 87 90 protected String header = null; 91 92 95 protected String footer = null; 96 97 101 protected String message = null; 102 103 public String getId() { 104 return (this.id); 105 } 106 107 public void setId(String id) { 108 this.id = id; 109 } 110 111 public String getBundle() { 112 return (this.bundle); 113 } 114 115 public void setBundle(String bundle) { 116 this.bundle = bundle; 117 } 118 119 public String getLocale() { 120 return (this.locale); 121 } 122 123 public void setLocale(String locale) { 124 this.locale = locale; 125 } 126 127 public String getName() { 128 return (this.name); 129 } 130 131 public void setName(String name) { 132 this.name = name; 133 } 134 135 public String getProperty() { 136 return (this.property); 137 } 138 139 public void setProperty(String property) { 140 this.property = property; 141 } 142 143 public String getHeader() { 144 return (this.header); 145 } 146 147 public void setHeader(String header) { 148 this.header = header; 149 } 150 151 public String getFooter() { 152 return (this.footer); 153 } 154 155 public void setFooter(String footer) { 156 this.footer = footer; 157 } 158 159 public String getMessage() { 160 return (this.message); 161 } 162 163 public void setMessage(String message) { 164 this.message = message; 165 } 166 167 173 public int doStartTag() throws JspException { 174 processed = false; 176 177 ActionMessages messages = null; 179 180 String name = this.name; 182 183 if (message != null && "true".equalsIgnoreCase(message)) { 184 name = Globals.MESSAGE_KEY; 185 } 186 187 try { 188 messages = TagUtils.getInstance().getActionMessages(pageContext, name); 189 190 } catch (JspException e) { 191 TagUtils.getInstance().saveException(pageContext, e); 192 throw e; 193 } 194 195 this.iterator = (property == null) ? messages.get() : messages.get(property); 197 198 if (!this.iterator.hasNext()) { 200 return SKIP_BODY; 201 } 202 203 ActionMessage report = (ActionMessage) this.iterator.next(); 204 String msg = null; 205 if (report.isResource()) { 206 msg = TagUtils.getInstance().message( 207 pageContext, 208 bundle, 209 locale, 210 report.getKey(), 211 report.getValues()); 212 } else { 213 msg = report.getKey(); 214 } 215 216 if (msg == null) { 217 pageContext.removeAttribute(id); 218 } else { 219 pageContext.setAttribute(id, msg); 220 } 221 222 if (header != null && header.length() > 0) { 223 String headerMessage = 224 TagUtils.getInstance().message(pageContext, bundle, locale, header); 225 226 if (headerMessage != null) { 227 TagUtils.getInstance().write(pageContext, headerMessage); 228 } 229 } 230 231 processed = true; 234 235 return (EVAL_BODY_TAG); 236 } 237 238 244 public int doAfterBody() throws JspException { 245 if (bodyContent != null) { 247 TagUtils.getInstance().writePrevious(pageContext, bodyContent.getString()); 248 bodyContent.clearBody(); 249 } 250 251 if (iterator.hasNext()) { 253 ActionMessage report = (ActionMessage) iterator.next(); 254 String msg = 255 TagUtils.getInstance().message( 256 pageContext, 257 bundle, 258 locale, 259 report.getKey(), 260 report.getValues()); 261 262 if (msg == null) { 263 pageContext.removeAttribute(id); 264 } else { 265 pageContext.setAttribute(id, msg); 266 } 267 268 return (EVAL_BODY_TAG); 269 } else { 270 return (SKIP_BODY); 271 } 272 273 } 274 275 276 281 public int doEndTag() throws JspException { 282 if (processed && footer != null && footer.length() > 0) { 283 284 String footerMessage = 285 TagUtils.getInstance().message(pageContext, bundle, locale, footer); 286 287 if (footerMessage != null) { 288 TagUtils.getInstance().write(pageContext, footerMessage); 289 } 290 } 291 292 return EVAL_PAGE; 293 } 294 295 296 299 public void release() { 300 super.release(); 301 iterator = null; 302 processed = false; 303 id = null; 304 bundle = null; 305 locale = Globals.LOCALE_KEY; 306 name = Globals.ERROR_KEY; 307 property = null; 308 header = null; 309 footer = null; 310 message = null; 311 } 312 313 } 314 | Popular Tags |