Search in sources :

Example 6 with FormBase

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);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) JspWriteRuntimeException(org.jaffa.presentation.portlet.widgets.taglib.exceptions.JspWriteRuntimeException) ActionMessages(org.apache.struts.action.ActionMessages) FormBase(org.jaffa.presentation.portlet.FormBase) Iterator(java.util.Iterator) ActionMessage(org.apache.struts.action.ActionMessage) OuterFormTagMissingRuntimeException(org.jaffa.presentation.portlet.widgets.taglib.exceptions.OuterFormTagMissingRuntimeException) IOException(java.io.IOException) JspWriter(javax.servlet.jsp.JspWriter)

Aggregations

FormBase (org.jaffa.presentation.portlet.FormBase)6 OuterFormTagMissingRuntimeException (org.jaffa.presentation.portlet.widgets.taglib.exceptions.OuterFormTagMissingRuntimeException)4 IOException (java.io.IOException)3 JspException (javax.servlet.jsp.JspException)3 JspWriter (javax.servlet.jsp.JspWriter)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 SimpleWidgetModel (org.jaffa.presentation.portlet.widgets.model.SimpleWidgetModel)2 JspWriteRuntimeException (org.jaffa.presentation.portlet.widgets.taglib.exceptions.JspWriteRuntimeException)2 WidgetModelAccessMethodNotFoundRuntimeException (org.jaffa.presentation.portlet.widgets.taglib.exceptions.WidgetModelAccessMethodNotFoundRuntimeException)2 Method (java.lang.reflect.Method)1 Iterator (java.util.Iterator)1 ServletRequest (javax.servlet.ServletRequest)1 ActionMessage (org.apache.struts.action.ActionMessage)1 ActionMessages (org.apache.struts.action.ActionMessages)1 FormKey (org.jaffa.presentation.portlet.FormKey)1 UserSession (org.jaffa.presentation.portlet.session.UserSession)1 MissingParametersRuntimeException (org.jaffa.presentation.portlet.widgets.taglib.exceptions.MissingParametersRuntimeException)1 WidgetModelAccessMethodInvocationRuntimeException (org.jaffa.presentation.portlet.widgets.taglib.exceptions.WidgetModelAccessMethodInvocationRuntimeException)1 IPropertyRuleIntrospector (org.jaffa.rules.IPropertyRuleIntrospector)1 RulesEngineException (org.jaffa.rules.RulesEngineException)1