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