import java.io.IOException;
import java.util.Iterator;
import java.util.Locale;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.util.MessageResources;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;
import org.apache.struts.taglib.Constants;
/**
* Check for a valid User logged on in the current session. If there is no
* such user, forward control to the login page.
*/
public final class MyErrorsTag extends TagSupport {
protected String name = Action.ERROR_KEY;
protected static MessageResources messages =
MessageResources.getMessageResources(Constants.Package +
".LocalStrings");
protected String locale = Action.LOCALE_KEY;
protected static Locale defaultLocale = Locale.getDefault();
protected String bundle = Action.MESSAGES_KEY;
public int doStartTag() throws JspException {
// Were any error messages specified?
ActionErrors errors = new ActionErrors();
try {
Object value = pageContext.getAttribute
(name, PageContext.REQUEST_SCOPE);
if (value == null) {
;
} else if (value instanceof String) {
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError((String) value));
} else if (value instanceof String[]) {
String keys[] = (String[]) value;
for (int i = 0; i < keys.length; i++)
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError(keys[i]));
/*} else if (value instanceof ErrorMessages) {
String keys[] = ((ErrorMessages) value).getErrors();
if (keys == null)
keys = new String[0];
for (int i = 0; i < keys.length; i++)
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError(keys[i]));
*/} else if (value instanceof ActionErrors) {
errors = (ActionErrors) value;
} else {
JspException e = new JspException
(messages.getMessage("errorsTag.errors",
value.getClass().getName()));
RequestUtils.saveException(pageContext, e);
throw e;
}
} catch (Exception e) {
;
}
if (errors.empty())
return (EVAL_BODY_INCLUDE);
// Check for presence of header and footer message keys
boolean headerPresent =
RequestUtils.present(pageContext, bundle, locale, "errors.header");
boolean footerPresent =
RequestUtils.present(pageContext, bundle, locale, "errors.footer");
// Render the error messages appropriately
StringBuffer results = new StringBuffer();
results.append("<script language=\"JavaScript\" type=\"text/javascript\">\r\n<!--\r\n");
results.append("window.alert(\"");
String message = null;
Iterator reports = errors.get();
while (reports.hasNext()) {
ActionError report = (ActionError) reports.next();
message = RequestUtils.message(pageContext, bundle,
locale, report.getKey(),
report.getValues());
if (message != null) {
results.append(message);
results.append("\\r\\n");
}
}
//results.delete(results.length()-2, 2); //"\r\n"
results.append("\");\r\n");
results.append("// -->\r\n</script>");
// Print the results to our output writer
ResponseUtils.write(pageContext, results.toString());
// Continue processing this page
return (EVAL_BODY_INCLUDE);
}
}
|