dgraham 2003/09/10 18:18:45 Modified: src/share/org/apache/struts/action ActionMessages.java RequestProcessor.java Action.java Log: Added ability to save ActionMessages in the session and have them cleaned up after the first use. This included adding back the version of Action.saveMessages() that stores them in the session. ActionMessages.isAccessed() returns true after the messages have been retrieved one time. RequestProcessor.processCachedMessages() queries isAccessed() to determine if it should remove the messages from the session. Revision Changes Path 1.12 +27 -9 jakarta-struts/src/share/org/apache/struts/action/ActionMessages.java Index: ActionMessages.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/action/ActionMessages.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- ActionMessages.java 19 Aug 2003 23:26:28 -0000 1.11 +++ ActionMessages.java 11 Sep 2003 01:18:45 -0000 1.12 @@ -108,11 +108,18 @@ * The "property name" marker to use for global messages, as opposed to * those related to a specific property. */ - public static final String GLOBAL_MESSAGE = "org.apache.struts.action.GLOBAL_MESSAGE"; + public static final String GLOBAL_MESSAGE = + "org.apache.struts.action.GLOBAL_MESSAGE"; // ----------------------------------------------------- Instance Variables /** + * Have the messages been retrieved from this object? + * @since Struts 1.2 + */ + protected boolean accessed = false; + + /** * The accumulated set of <code>ActionMessage</code> objects (represented * as an ArrayList) for each property, keyed by property name. */ @@ -207,9 +214,7 @@ * Clear all messages recorded by this object. */ public void clear() { - messages.clear(); - } /** @@ -227,7 +232,8 @@ * no messages recorded, an empty enumeration is returned. */ public Iterator get() { - + this.accessed = true; + if (messages.isEmpty()) { return Collections.EMPTY_LIST.iterator(); } @@ -261,7 +267,8 @@ * @param property Property name (or ActionMessages.GLOBAL_MESSAGE) */ public Iterator get(String property) { - + this.accessed = true; + ActionMessageItem item = (ActionMessageItem) messages.get(property); if (item == null) { @@ -270,6 +277,17 @@ return (item.getList().iterator()); } + } + + /** + * Returns <code>true</code> if the <code>get()</code> or + * <code>get(String)</code> methods are called. + * @return <code>true</code> if the messages have been accessed one or more + * times. + * @since Struts 1.2 + */ + public boolean isAccessed() { + return this.accessed; } /** 1.34 +43 -5 jakarta-struts/src/share/org/apache/struts/action/RequestProcessor.java Index: RequestProcessor.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/action/RequestProcessor.java,v retrieving revision 1.33 retrieving revision 1.34 diff -u -r1.33 -r1.34 --- RequestProcessor.java 23 Aug 2003 00:29:39 -0000 1.33 +++ RequestProcessor.java 11 Sep 2003 01:18:45 -0000 1.34 @@ -187,7 +187,6 @@ } - /** * <p>Process an <code>HttpServletRequest</code> and create the * corresponding <code>HttpServletResponse</code>.</p> @@ -210,6 +209,7 @@ if (path == null) { return; } + if (log.isDebugEnabled()) { log.debug("Processing a '" + request.getMethod() + "' for path '" + path + "'"); @@ -226,6 +226,8 @@ if (!processPreprocess(request, response)) { return; } + + this.processCachedMessages(request, response); // Identify the mapping for this request ActionMapping mapping = processMapping(request, response, path); @@ -249,6 +251,7 @@ if (!processForward(request, response, mapping)) { return; } + if (!processInclude(request, response, mapping)) { return; } @@ -452,6 +455,37 @@ } } + + /** + * Removes any ActionMessages object stored in the session under + * <code>Globals.MESSAGE_KEY</code> if the messages' + * <code>isAccessed()</code> method returns true. This allows messages to + * be stored in the session, display one time, and get cleaned up here. + * @param request The servlet request we are processing. + * @param response The servlet response we are creating. + * @since Struts 1.2 + */ + protected void processCachedMessages( + HttpServletRequest request, + HttpServletResponse response) { + + HttpSession session = request.getSession(false); + if (session == null) { + return; + } + + ActionMessages messages = + (ActionMessages) session.getAttribute(Globals.MESSAGE_KEY); + + if (messages == null) { + return; + } + + if (messages.isAccessed()) { + session.removeAttribute(Globals.MESSAGE_KEY); + } + + } /** @@ -776,18 +810,22 @@ if (log.isDebugEnabled()) { log.debug(" Populating bean properties from this request"); } + form.setServlet(this.servlet); form.reset(mapping, request); + if (mapping.getMultipartClass() != null) { request.setAttribute(Globals.MULTIPART_KEY, mapping.getMultipartClass()); } + RequestUtils.populate(form, mapping.getPrefix(), mapping.getSuffix(), request); // Set the cancellation request attribute if appropriate if ((request.getParameter(Constants.CANCEL_PROPERTY) != null) || (request.getParameter(Constants.CANCEL_PROPERTY_X) != null)) { + request.setAttribute(Globals.CANCEL_KEY, Boolean.TRUE); } 1.70 +30 -4 jakarta-struts/src/share/org/apache/struts/action/Action.java Index: Action.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/action/Action.java,v retrieving revision 1.69 retrieving revision 1.70 diff -u -r1.69 -r1.70 --- Action.java 10 Sep 2003 02:28:38 -0000 1.69 +++ Action.java 11 Sep 2003 01:18:45 -0000 1.70 @@ -472,6 +472,32 @@ // Save the messages we need request.setAttribute(Globals.MESSAGE_KEY, messages); } + + /** + * Save the specified messages keys into the appropriate session + * attribute for use by the <html:messages> tag (if + * messages="true" is set), if any messages are required. Otherwise, + * ensure that the session attribute is not created. + * + * @param session The session to save the messages in. + * @param messages The messages to save. <code>null</code> or empty + * messages removes any existing ActionMessages in the session. + * + * @since Struts 1.2 + */ + protected void saveMessages( + HttpSession session, + ActionMessages messages) { + + // Remove any messages attribute if none are required + if ((messages == null) || messages.isEmpty()) { + session.removeAttribute(Globals.MESSAGE_KEY); + return; + } + + // Save the messages we need + session.setAttribute(Globals.MESSAGE_KEY, messages); + } /** * Save a new transaction token in the user's current session, creating
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]