struts-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg V Alexeev <go...@penza.net>
Subject extended error processing
Date Mon, 23 Oct 2000 22:42:07 GMT
Hello struts-dev,

  I develop some classes to support more flexible error processing in
  struts. May be it can be useful for anybody else.

  On base of ErrorsTag I create ExtErrorsTag class to show errors more
  flexible - with ability to show property names and messages for this
  properties.


> Class to support access to property names in errors container -
  
>---------------------------------------------------------------------
package org.apache.struts.action;

import java.util.Iterator;

public class ActionErrorsExt extends ActionErrors {

    // ------------------------------------------- Public Methods


    /**
     * Add an error message to the set of errors for default property
     * ActionErrors.GLOBAL_ERROR .
     *
     * @param error The error message to be added
     */
    public void add( ActionError error) {
     add( ActionErrors.GLOBAL_ERROR, error );
    }

    /**
     * Return Iterator for property names.
     *
     */
    public Iterator getProperties() {
     return errors.keySet().iterator();
    }
 

}
>---------------------------------------------------------------------

> Base class for errors processing tag handlers

>---------------------------------------------------------------------
package org.apache.struts.taglib.errors;

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.action.ActionErrorsExt;
import org.apache.struts.util.BeanUtils;
import org.apache.struts.util.ErrorMessages;
import org.apache.struts.util.MessageResources;

public class BaseErrorsTag extends TagSupport {

    // --------------------------------------------------- Static Variables

    protected static String ERRORS_HEADER_KEY =
     "org.apache.struts.errors.header";

    protected static String ERRORS_FOOTER_KEY = 
     "org.apache.struts.errors.footer";

    protected static String ERRORS_PROP_HEADER_KEY = 
     "org.apache.struts.errors.prop.header";

    protected static String ERRORS_PROP_FOOTER_KEY = 
     "org.apache.struts.errors.prop.footer";

    protected static String ERRORS_PROP_NAME_HEADER_KEY = 
     "org.apache.struts.errors.prop.name.header";

    protected static String ERRORS_PROP_NAME_FOOTER_KEY = 
     "org.apache.struts.errors.prop.name.footer";

    protected static String ERRORS_PROP_MESSAGES_HEADER_KEY =
     "org.apache.struts.errors.prop.messages.header";

    protected static String ERRORS_PROP_MESSAGES_FOOTER_KEY =
     "org.apache.struts.errors.prop.messages.footer";

    protected static String ERRORS_PROP_MESSAGE_HEADER_KEY = 
     "org.apache.struts.errors.prop.message.header";

    protected static String ERRORS_PROP_MESSAGE_FOOTER_KEY = 
     "org.apache.struts.errors.prop.message.footer";

    // --------------------------------------------------- Instance Variables


    /**
     * The default locale on our server.
     */
    private static Locale defaultLocale = Locale.getDefault();


    /**
     * Name of the request scope attribute containing our error messages,
     * if any.
     */
    private String name = Action.ERROR_KEY;


    /**
     * Display key if message not exist.
     */
    private String flexible = null;

    // --------------------------------------------------- Constructors

    public BaseErrorsTag() {
     super();
    }

    // ---------------------------------------------------- Protected Methods

    protected ActionErrorsExt storedErrors() {
        ActionErrorsExt errors = new ActionErrorsExt();
        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 = (ActionErrorsExt) value;
            }
        } catch (Exception e) {
            ;
        }
        return errors;
    }


    protected Locale getLocale() {
        Locale locale = null;
        try {
            locale = (Locale) pageContext.getAttribute
                (Action.LOCALE_KEY, PageContext.SESSION_SCOPE);
        } catch (IllegalStateException e) {     // Invalidated session
            locale = null;
        }
        if (locale == null)
            locale = defaultLocale;
        return locale;
    }


    // ----------------------------------------------------------- Properties

    /**
     * Return the errors attribute name.
     */
    public String getName() {

        return (this.name);

    }

    /**
     * Set the errors attribute name.
     *
     * @param name The new errors attribute name
     */
    public void setName(String name) {

        this.name = name;

    }


    /**
     * Return the errors attribute flexible.
     */
    public String getFlexible() {

        return (this.flexible);

    }

    /**
     * Set the errors attribute flexible.
     *
     * @param flexible The new errors attribute flexible
     */
    public void setFlexible(String flexible) {

        this.flexible = flexible;

    }

    // ------------------------------------------------------- Public Methods

    /**
     * Release any acquired resources.
     */
    public void release() {

        super.release();
        name = Action.ERROR_KEY;
        flexible = null;

    }

}

>-------------------------------------------------------------

> Tag handler for extended error processing

>-------------------------------------------------------------

package org.apache.struts.taglib.errors;

import java.util.Iterator;
import java.io.IOException;
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.action.ActionErrorsExt;
import org.apache.struts.util.BeanUtils;
import org.apache.struts.util.MessageResources;

public class ExtErrorsTag extends BaseErrorsTag {

    // --------------------------------------------------- Constructors

    public ExtErrorsTag() {
     super();
    }
 
    // ------------------------------------------------------- Public Methods


    /**
     * Render the specified error messages if there are any.
     *
     * @exception JspException if a JSP exception has occurred
     */
    public int doStartTag() throws JspException {
     ActionErrorsExt actionErrors = (ActionErrorsExt)storedErrors();
     if (actionErrors.empty())
      return (EVAL_BODY_INCLUDE);
     String             property = null;
     Locale             locale = getLocale();
     MessageResources   messages = (MessageResources)
      pageContext.getAttribute(Action.MESSAGES_KEY,
      PageContext.APPLICATION_SCOPE);
     StringBuffer       results = new StringBuffer();
     String errorPropHeader =
      messages.getMessage(locale, ERRORS_PROP_HEADER_KEY );
     String errorPropFooter =
      messages.getMessage(locale, ERRORS_PROP_FOOTER_KEY );
     String errorPropNameHeader =
      messages.getMessage(locale, ERRORS_PROP_NAME_HEADER_KEY );
     String errorPropNameFooter =
      messages.getMessage(locale, ERRORS_PROP_NAME_FOOTER_KEY );
     String errorPropMessagesHeader =
      messages.getMessage(locale, ERRORS_PROP_MESSAGES_HEADER_KEY );
     String errorPropMessagesFooter =
      messages.getMessage(locale, ERRORS_PROP_MESSAGES_FOOTER_KEY );
     String errorPropMessageHeader =
      messages.getMessage(locale, ERRORS_PROP_MESSAGE_HEADER_KEY );
     String errorPropMessageFooter =
      messages.getMessage(locale, ERRORS_PROP_MESSAGE_FOOTER_KEY );
     String message =
      messages.getMessage(locale, ERRORS_HEADER_KEY );
     if (message != null)
      results.append(message);
     Iterator   reports = null;
     Iterator   properties = actionErrors.getProperties();
     while( properties.hasNext() ) {
      if( errorPropHeader!=null ) results.append( errorPropHeader );
      if( errorPropNameHeader!=null ) results.append( errorPropNameHeader );
      property = (String)properties.next();
      message = messages.getMessage( locale, property );
      if( message!=null )
       results.append( message );
      else
       if( getFlexible()!=null )
        results.append( property );
      if( errorPropNameFooter!=null ) results.append( errorPropNameFooter );
      if( errorPropMessagesHeader!=null ) results.append( errorPropMessagesHeader );
      reports = actionErrors.get( property );
      while( reports.hasNext() ) {
       if( errorPropMessageHeader!=null ) results.append( errorPropMessageHeader );
       ActionError report = (ActionError) reports.next();
       message = messages.getMessage(locale,
                 report.getKey(), report.getValues());
       if( message!=null )
        results.append( message );
       else
        if( getFlexible()!=null )
         results.append( report.getKey() );
       if( errorPropMessageFooter!=null ) results.append( errorPropMessageFooter );
      }
      if( errorPropMessagesFooter!=null ) results.append( errorPropMessagesFooter );
      if( errorPropFooter!=null ) results.append( errorPropFooter );
     }
     message = messages.getMessage(locale, ERRORS_FOOTER_KEY );
     if( message!=null )
      results.append( message );
     JspWriter writer = pageContext.getOut();
     try {
      writer.print(results.toString());
     } catch (IOException e) {
        throw new JspException(e.toString());
     }
     return (EVAL_BODY_INCLUDE);
    }


}

>-------------------------------------------------------------

> Resource strings sample

>-------------------------------------------------------------

org.apache.struts.errors.header=<table><tr><td colspan=2>Errors detected!</td></tr>
org.apache.struts.errors.footer=</table>
org.apache.struts.errors.prop.header=<tr>
org.apache.struts.errors.prop.footer=</tr>
org.apache.struts.errors.prop.name.header=<td>
org.apache.struts.errors.prop.name.footer=</td>
org.apache.struts.errors.prop.messages.header=<td>
org.apache.struts.errors.prop.messages.footer=</td>
org.apache.struts.errors.prop.message.header=<li>
org.apache.struts.errors.prop.message.footer=</li>


-- 
Best regards,
 Oleg                          mailto:gonza@penza.net



Mime
View raw message