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/ActionMessagesTool The comment on the change is: Orphaned; no documentation besides, just a big chunk of code. ------------------------------------------------------------------------------ - July 24, 2003 + deleted - Here is an updated version of the ActionMessagesTool, compatible with Struts 1.1 ... it now calls the new .getMessageResources() in StrutsUtils. - - Any comments are welcome on the Velocity Developer's List <velocity-dev@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.ActionMessage; - import org.apache.struts.action.ActionMessages; - - import org.apache.velocity.app.Velocity; - import org.apache.velocity.tools.struts.StrutsUtils; - import org.apache.velocity.tools.view.context.ViewContext; - import org.apache.velocity.tools.view.tools.ViewTool; - - /** - {{{ * <p>View tool to work with the Struts action messages.</p> - * - * <p>This class is equipped to be used with a toolbox manager, for example - * the ServletToolboxManager included with VelocityViewServlet. This class - * implements interface ViewTool, which allows a toolbox manager to pass the - * required context information.</p> - * - * <p>This means this tool should only be used in the request scope.</p> - * - * @author <a href="mailto:[EMAIL PROTECTED]">Gabe Sidler</a> - * @author <a href="mailto:[EMAIL PROTECTED]">Nathan Bubna</a> - * - * @version $Id: ActionMessagesTool.java,v 1.1 2003/07/22 06:09:22 nbubna Exp $ - */ }}} - public class ActionMessagesTool implements ViewTool - { - - {{{ /** 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 ActionMessages actionMsgs; - - - /** - * Default constructor. Tool must be initialized before use. - */ - public ActionMessagesTool() - {} - - - /** - * 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; - HttpServletRequest request = context.getRequest(); - HttpSession session = request.getSession(false); - ServletContext application = context.getServletContext(); - - this.locale = StrutsUtils.getLocale(request, session); - this.resources = StrutsUtils.getMessageResources(request, application); - this.actionMsgs = StrutsUtils.getActionMessages(request); - } - - - /*************************** Public Methods ***********************/ - - /** - * <p>Returns <code>true</code> if there are action messages queued, - * otherwise <code>false</code>.</p> - */ - public boolean exist() - { - if (actionMsgs == null) - { - return false; - } - return !actionMsgs.isEmpty(); - } - - - /** - * <p>Returns true if there are action messages queued for the specified - * category of messages, otherwise <code>false</code>.</p> - * - * @param property the category of messages to check for - */ - public boolean exist(String property) - { - if (actionMsgs == null) - { - return false; - } - return (actionMsgs.size(property) > 0); - } - - - /** - * Returns the number of action messages queued. - */ - public int getSize() - { - if (actionMsgs == null) - { - return 0; - } - return actionMsgs.size(); - } - - - /** - * Returns the number of action messages queued for a particular property. - * - * @param property the category of messages to check for - */ - public int getSize(String property) - { - if (actionMsgs == null) - { - return 0; - } - return actionMsgs.size(property); - } - - - /** - * Returns the set of localized action messages as an - * <code>java.util.ArrayList</code> of <code> java.lang.String</code> - * for all actionMsgs queued or <code>null</code> if no messages are queued. - * If the message resources don't contain a message for a - * particular key, the key itself is used as the 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 actionMsgs 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 a message for a particular key, - * the key itself is used as the message. - * - * @param property the category of actionMsgs to operate on - */ - public ArrayList get(String property) - { - if (actionMsgs == null || actionMsgs.isEmpty()) - { - return null; - } - - if (resources == null) - { - Velocity.error("Message resources are not available."); - //FIXME? should we return the list of message keys instead? - return null; - } - - Iterator msgs; - if (property == null) - { - msgs = actionMsgs.get(); - } - else - { - msgs = actionMsgs.get(property); - } - - if (!msgs.hasNext()) - { - return null; - } - - ArrayList list = new ArrayList(); - - while (msgs.hasNext()) - { - ActionMessage msg = (ActionMessage)msgs.next(); - String message = resources.getMessage(locale, - msg.getKey(), - msg.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 " + msg.getKey() + - " could not be found in message resources."); - list.add(msg.getKey()); - } - } - return list; - } }}} - } - - {{{ } } } - }}} - --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]