use of org.jaffa.presentation.portlet.FormBase in project jaffa-framework by jaffa-projects.
the class RaiseErrorsTag method otherDoEndTagOperations.
/**
* This generates the HTML for the tag.
* Writes a script tag containing error message details to be added to the current jsp.
*
* @throws JspException if any error occurs.
*/
public void otherDoEndTagOperations() throws JspException {
// Get the form bean from the page.
FormBase form = TagHelper.getFormBase(pageContext);
if (form == null) {
String str = "The " + TAG_NAME + " should be inside a FormTag";
log.error(str);
throw new OuterFormTagMissingRuntimeException(str);
}
// get the errors from the form
if (!FormBase.hasErrors((HttpServletRequest) pageContext.getRequest())) {
return;
}
ActionMessages errors = FormBase.getErrors((HttpServletRequest) pageContext.getRequest());
StringBuilder buf = new StringBuilder();
buf.append("<SCRIPT type=\"text/javascript\">");
for (Iterator itr = errors.get(); itr.hasNext(); ) {
ActionMessage error = (ActionMessage) itr.next();
buf.append("addMessage(\"");
String messageHtml = StringHelper.convertToHTML(MessageHelper.findMessage(pageContext, error.getKey(), error.getValues()));
String labelEditorLink = TagHelper.getLabelEditorLink(pageContext, error.getKey());
// escape the message for html to prevent XSS
String message = Encode.forHtml(messageHtml) + labelEditorLink;
buf.append(message);
buf.append("\");");
}
// delete the error so that it doesn't get re-displayed
form.clearErrors((HttpServletRequest) pageContext.getRequest());
buf.append("</SCRIPT>");
// add the script tag containing the error message to the current jsp
try {
JspWriter w = pageContext.getOut();
w.print(buf.toString());
} catch (IOException e) {
String str = "Exception in writing the " + TAG_NAME;
log.error(str, e);
throw new JspWriteRuntimeException(str, e);
}
}
Aggregations