You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Apache Wiki <wi...@apache.org> on 2006/02/16 23:45:25 UTC
[Struts Wiki] Update of "ErrorsTool" by MichaelJouravlev
Dear Wiki user,
You have subscribed to a wiki page or wiki category on "Struts Wiki" for change notification.
The following page has been changed by MichaelJouravlev:
http://wiki.apache.org/struts/ErrorsTool
The comment on the change is:
Orphaned; no docs, only the code.
------------------------------------------------------------------------------
- July 24, 2003
+ deleted
- Here is an updated version of the ErrorsTool, compatible with Struts 1.1 ... I only had to change .empty() to .isEmpty() and call the new getMessageResources() in StrutsUtils.
-
- Any comments are welcome on the Velocity Developer's List <ve...@jakarta.apache.org>.
-
- Marinó A. Jónsson
-
- ----
- {{{
- { { {
-
- package org.apache.velocity.tools.struts;
-
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.Locale;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
- import javax.servlet.ServletContext;
-
- import org.apache.struts.util.MessageResources;
- import org.apache.struts.action.*;
-
- import org.apache.velocity.app.Velocity;
- import org.apache.velocity.tools.view.context.ViewContext;
- import org.apache.velocity.tools.view.tools.ViewTool;
-
-
- /**
- {{{ * <p>View tool to work with the Struts error messages.</p>
- *
- * <p>This class is equipped to be used with a toolbox manager, for example
- * the ServletToolboxManager included with VelServlet. This class implements
- * interface ViewTool, which allows a toolbox manager to pass the required
- * context information.</p>
- *
- * <p>This class is not thread-safe by design. A new instance is needed for
- * the processing of every template request. This means this tool should
- * only be used in the request scope, not application or session scopes.</p>
- *
- * @author <a href="mailto:sidler@teamup.com">Gabe Sidler</a>
- *
- * @version $Id: ErrorsTool.java,v 1.4 2003/05/28 00:17:15 nbubna Exp $
- */ }}}
- public class ErrorsTool implements ViewTool
- {
-
- {{{ // --------------------------------------------- Properties ---------------
-
- /**
- * A reference to the ServletContext
- */
- protected ServletContext application;
-
-
- /**
- * A reference to the HttpServletRequest.
- */
- protected HttpServletRequest request;
-
-
- /**
- * A reference to the HttpSession.
- */
- protected HttpSession session;
-
-
- /**
- * A reference to the Struts message resources.
- */
- protected MessageResources resources;
-
-
- /**
- * A reference to the user's locale.
- */
- protected Locale locale;
-
-
- /**
- * A reference to the queued action messages.
- */
- protected ActionErrors errors;
-
-
-
- // --------------------------------------------- Constructors -------------
-
- /**
- * Default constructor. Tool must be initialized before use.
- */
- public ErrorsTool()
- {}
-
-
- /**
- * Initializes this tool.
- *
- * @param obj the current ViewContext
- * @throws IllegalArgumentException if the param is not a ViewContext
- */
- public void init(Object obj)
- {
- if (!(obj instanceof ViewContext))
- {
- throw new IllegalArgumentException("Tool can only be initialized with a ViewContext");
- }
-
- ViewContext context = (ViewContext)obj;
- this.request = context.getRequest();
- this.session = request.getSession(false);
- this.application = context.getServletContext();
-
- resources = StrutsUtils.getMessageResources(request, application);
- locale = StrutsUtils.getLocale(request, session);
- errors = StrutsUtils.getActionErrors(request);
- }
-
-
- // --------------------------------------------- View Helpers -------------
-
- /**
- * <p>Returns <code>true</code> if there are action errors queued,
- * otherwise <code>false</code>.</p>
- */
- public boolean exist()
- {
- if (errors == null)
- {
- return false;
- }
-
- return !errors.isEmpty();
- }
-
-
- /**
- * <p>Returns true if there are action errors queued for the specified
- * category of errors, otherwise <code>false</code>.</p>
- *
- * @param property the category of errors to check for
- */
- public boolean exist(String property)
- {
- if (errors == null)
- {
- return false;
- }
- return (errors.size(property) > 0);
- }
-
-
- /**
- * Returns the number of action errors queued.
- */
- public int getSize()
- {
- if (errors == null)
- {
- return 0;
- }
-
- return errors.size();
- }
-
-
- /**
- * Returns the number of action errors queued for a particular property.
- *
- * @param property the category of errors to check for
- */
- public int getSize(String property)
- {
- if (errors == null)
- {
- return 0;
- }
-
- return errors.size(property);
- }
-
-
- /**
- * <p>
- * This a convenience method and the equivalent of
- * <code>$errors.get($errors.globalName)</code>.
- * </p>
- * <p>
- * Returns the set of localized error messages as an
- * <code>java.util.ArrayList</code> of <code> java.lang.String</code>
- * for all errors queued of the global category or <code>null</code>
- * if no error are queued for the specified category.
- * </p>
- * <p>
- * If the message resources don't contain an error message for a
- * particular error key, the key itself is used as error message.
- * </p>
- */
- public ArrayList getGlobal()
- {
- return get(getGlobalName());
- }
-
-
- /**
- * Returns the set of localized error messages as an
- * <code>java.util.ArrayList</code> of <code> java.lang.String</code>
- * for all errors queued or <code>null</code> if no errors are queued.
- * If the message resources don't contain an error message for a
- * particular error key, the key itself is used as error message.
- */
- public ArrayList getAll()
- {
- return get(null);
- }
-
-
- /**
- * Returns the set of localized error messages as an
- * <code>java.util.ArrayList</code> of <code> java.lang.String</code>
- * for all errors queued of the specified category or <code>null</code>
- * if no error are queued for the specified category. If the message
- * resources don't contain an error message for a particular error key,
- * the key itself is used as error message.
- *
- * @param property the category of errors to operate on
- */
- public ArrayList get(String property)
- {
- if (errors == null || errors.isEmpty())
- {
- return null;
- }
-
- if (resources == null)
- {
- Velocity.error("Message resources are not available.");
- //FIXME? should we return the list of error keys instead?
- return null;
- }
-
- Iterator errormsgs;
- if (property == null)
- {
- errormsgs = errors.get();
- }
- else
- {
- errormsgs = errors.get(property);
- }
-
- if (!(errormsgs.hasNext()))
- {
- return null;
- }
-
- ArrayList list = new ArrayList();
-
- while (errormsgs.hasNext())
- {
- ActionError errormsg = (ActionError)errormsgs.next();
- String message = resources.getMessage(locale,
- errormsg.getKey(),
- errormsg.getValues());
- if (message != null)
- {
- list.add(message);
- }
- else
- {
- // if error message cannot be found for a key, return key instead
- Velocity.warn("Message for key \'''''' + errormsg.getKey() + "\" could not be found.");
- list.add(errormsg.getKey());
- }
- }
- return list;
- }
-
-
- /**
- * <p>Renders the queued error messages as a list. This method expects
- * the message keys <code>errors.header</code> and <code>errors.footer</code>
- * in the message resources. The value of the former is rendered before
- * the list of error messages and the value of the latter is rendered
- * after the error messages.</p>
- *
- * @return The formatted error messages. If no error messages are queued,
- * an empty string is returned.
- */
- public String getMsgs()
- {
- return StrutsUtils.errorMarkup(null, request, session, application);
- }
-
-
- /**
- * <p>Renders the queued error messages of a particual category as a list.
- * This method expects the message keys <code>errors.header</code> and
- * <code>errors.footer</code> in the message resources. The value of the
- * former is rendered before the list of error messages and the value of
- * the latter is rendered after the error messages.</p>
- *
- * @param property the category of errors to render
- *
- * @return The formatted error messages. If no error messages are queued,
- * an empty string is returned.
- */
- public String getMsgs(String property)
- {
- return StrutsUtils.errorMarkup(property, request, session, application);
- }
-
-
- /**
- * Returns the default "GLOBAL" category name that can be used for
- * messages that are not associated with a particular property.
- */
- public String getGlobalName()
- {
- return StrutsUtils.getGlobalErrorName();
- } }}}
- }
-
- {{{ } } }
- }}}
-
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org