craigmcc 01/04/18 16:32:35 Modified: src/share/org/apache/struts/taglib/html ErrorsTag.java src/share/org/apache/struts/util MessageResources.java RequestUtils.java Log: Make it possible to omit the "errors.header" and "errors.footer" message keys in your application resources, if you do not want them. Previously, the <html:errors> tag would display "missing message" errors for these keys if you set the "null" initialization parameter for the controller servlet to false. PR: Bugzilla #1036 Submitted by: Wes Price <[EMAIL PROTECTED]> Revision Changes Path 1.8 +18 -8 jakarta-struts/src/share/org/apache/struts/taglib/html/ErrorsTag.java Index: ErrorsTag.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/ErrorsTag.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- ErrorsTag.java 2001/02/20 03:11:20 1.7 +++ ErrorsTag.java 2001/04/18 23:32:34 1.8 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/ErrorsTag.java,v 1.7 2001/02/20 03:11:20 craigmcc Exp $ - * $Revision: 1.7 $ - * $Date: 2001/02/20 03:11:20 $ + * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/ErrorsTag.java,v 1.8 2001/04/18 23:32:34 craigmcc Exp $ + * $Revision: 1.8 $ + * $Date: 2001/04/18 23:32:34 $ * * ==================================================================== * @@ -98,7 +98,7 @@ * </ul> * * @author Craig R. McClanahan - * @version $Revision: 1.7 $ $Date: 2001/02/20 03:11:20 $ + * @version $Revision: 1.8 $ $Date: 2001/04/18 23:32:34 $ */ public class ErrorsTag extends TagSupport { @@ -225,10 +225,18 @@ if (errors.empty()) return (EVAL_BODY_INCLUDE); + // Check for presence of header and footer message keys + boolean headerPresent = + RequestUtils.present(pageContext, bundle, locale, "errors.header"); + boolean footerPresent = + RequestUtils.present(pageContext, bundle, locale, "errors.footer"); + // Render the error messages appropriately StringBuffer results = new StringBuffer(); - String message = RequestUtils.message(pageContext, bundle, - locale, "errors.header"); + String message = null; + if (headerPresent) + message = RequestUtils.message(pageContext, bundle, + locale, "errors.header"); if (message != null) { results.append(message); results.append("\r\n"); @@ -248,8 +256,10 @@ results.append("\r\n"); } } - message = RequestUtils.message(pageContext, bundle, - locale, "errors.footer"); + message = null; + if (footerPresent) + message = RequestUtils.message(pageContext, bundle, + locale, "errors.footer"); if (message != null) { results.append(message); results.append("\r\n"); 1.10 +38 -4 jakarta-struts/src/share/org/apache/struts/util/MessageResources.java Index: MessageResources.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/util/MessageResources.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- MessageResources.java 2001/02/12 00:32:13 1.9 +++ MessageResources.java 2001/04/18 23:32:35 1.10 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/util/MessageResources.java,v 1.9 2001/02/12 00:32:13 craigmcc Exp $ - * $Revision: 1.9 $ - * $Date: 2001/02/12 00:32:13 $ + * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/util/MessageResources.java,v 1.10 2001/04/18 23:32:35 craigmcc Exp $ + * $Revision: 1.10 $ + * $Date: 2001/04/18 23:32:35 $ * * ==================================================================== * @@ -89,7 +89,7 @@ * application server environments. * * @author Craig R. McClanahan - * @version $Revision: 1.9 $ $Date: 2001/02/12 00:32:13 $ + * @version $Revision: 1.10 $ $Date: 2001/04/18 23:32:35 $ */ public abstract class MessageResources implements Serializable { @@ -416,6 +416,40 @@ args[2] = arg2; args[3] = arg3; return (getMessage(locale, key, args)); + + } + + + /** + * Return <code>true</code> if there is a defined message for the specified + * key in the system default locale. + * + * @param key The message key to look up + */ + public boolean isPresent(String key) { + + return (isPresent(null, key)); + + } + + + /** + * Return <code>true</code> if there is a defined message for the specified + * key in the specified Locale. + * + * @param locale The requested message Locale, or <code>null</code> + * for the system default Locale + * @param key The message key to look up + */ + public boolean isPresent(Locale locale, String key) { + + String message = getMessage(locale, key); + if (message == null) + return (false); + else if (message.startsWith("???") && message.endsWith("???")) + return (false); // FIXME - Only valid for default implementation + else + return (true); } 1.9 +47 -4 jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java Index: RequestUtils.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- RequestUtils.java 2001/03/11 00:58:45 1.8 +++ RequestUtils.java 2001/04/18 23:32:35 1.9 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java,v 1.8 2001/03/11 00:58:45 craigmcc Exp $ - * $Revision: 1.8 $ - * $Date: 2001/03/11 00:58:45 $ + * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java,v 1.9 2001/04/18 23:32:35 craigmcc Exp $ + * $Revision: 1.9 $ + * $Date: 2001/04/18 23:32:35 $ * * ==================================================================== * @@ -89,7 +89,7 @@ * in the Struts controller framework. * * @author Craig R. McClanahan - * @version $Revision: 1.8 $ $Date: 2001/03/11 00:58:45 $ + * @version $Revision: 1.9 $ $Date: 2001/04/18 23:32:35 $ */ public class RequestUtils { @@ -492,6 +492,49 @@ } catch (Exception e) { throw new ServletException("BeanUtils.populate", e); } + + } + + + /** + * Return true if a message string for the specified message key + * is present for the specified Locale. + * + * @param pageContext The PageContext associated with this request + * @param bundle Name of the servlet context attribute for our + * message resources bundle + * @param locale Name of the session attribute for our user's Locale + * @param key Message key to be looked up and returned + * + * @exception JspException if a lookup error occurs (will have been + * saved in the request already) + */ + public static boolean present(PageContext pageContext, String bundle, + String locale, String key) + throws JspException { + + // Look up the requested MessageResources + if (bundle == null) + bundle = Action.MESSAGES_KEY; + MessageResources resources = (MessageResources) + pageContext.getAttribute(bundle, PageContext.APPLICATION_SCOPE); + if (resources == null) { + JspException e = new JspException + (messages.getMessage("message.bundle", bundle)); + saveException(pageContext, e); + throw e; + } + + // Look up the requested Locale + if (locale == null) + locale = Action.LOCALE_KEY; + Locale userLocale = (Locale) + pageContext.getAttribute(locale, PageContext.SESSION_SCOPE); + if (userLocale == null) + userLocale = defaultLocale; + + // Return the requested message presence indicator + return (resources.isPresent(userLocale, key)); }