You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by nb...@apache.org on 2003/10/20 04:23:25 UTC
cvs commit: jakarta-velocity-tools/src/java/org/apache/velocity/tools/struts ErrorsTool.java
nbubna 2003/10/19 19:23:25
Modified: src/java/org/apache/velocity/tools/struts ErrorsTool.java
Log:
add message resource bundle support to ErrorsTool
Revision Changes Path
1.6 +75 -11 jakarta-velocity-tools/src/java/org/apache/velocity/tools/struts/ErrorsTool.java
Index: ErrorsTool.java
===================================================================
RCS file: /home/cvs/jakarta-velocity-tools/src/java/org/apache/velocity/tools/struts/ErrorsTool.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- ErrorsTool.java 25 Jul 2003 16:54:40 -0000 1.5
+++ ErrorsTool.java 20 Oct 2003 02:23:25 -0000 1.6
@@ -161,6 +161,33 @@
}
+ /**
+ * Retrieves the specified {@link MessageResources} bundle, or the
+ * application's default MessageResources if no bundle is specified.
+ */
+ protected MessageResources getResources(String bundle)
+ {
+ if (bundle == null)
+ {
+ if (resources == null)
+ {
+ Velocity.error("Message resources are not available.");
+ }
+ return resources;
+ }
+
+ MessageResources res =
+ StrutsUtils.getMessageResources(request, application, bundle);
+ if (res == null)
+ {
+ Velocity.error("MessageResources bundle '" + bundle +
+ "' is not available.");
+ }
+ return res;
+ }
+
+
+
// --------------------------------------------- View Helpers -------------
/**
@@ -255,7 +282,7 @@
*/
public ArrayList getAll()
{
- return get(null);
+ return get(null, null);
}
@@ -271,14 +298,31 @@
*/
public ArrayList get(String property)
{
+ return get(property, 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
+ * @param bundle the message resource bundle to use
+ */
+ public ArrayList get(String property, String bundle)
+ {
if (errors == null || errors.isEmpty())
{
return null;
}
+ MessageResources res = getResources(bundle);
if (resources == null)
{
- Velocity.error("Message resources are not available.");
//FIXME? should we return the list of error keys instead?
return null;
}
@@ -299,13 +343,12 @@
}
ArrayList list = new ArrayList();
-
while (errormsgs.hasNext())
{
ActionError errormsg = (ActionError)errormsgs.next();
- String message = resources.getMessage(locale,
- errormsg.getKey(),
- errormsg.getValues());
+ String message = res.getMessage(locale,
+ errormsg.getKey(),
+ errormsg.getValues());
if (message != null)
{
list.add(message);
@@ -313,7 +356,8 @@
else
{
// if error message cannot be found for a key, return key instead
- Velocity.warn("Message for key \"" + errormsg.getKey() + "\" could not be found.");
+ Velocity.warn("Message for key \"" + errormsg.getKey() +
+ "\" could not be found.");
list.add(errormsg.getKey());
}
}
@@ -333,9 +377,9 @@
*/
public String getMsgs()
{
- return StrutsUtils.errorMarkup(null, request, session, application);
+ return getMsgs(null, null);
}
-
+
/**
* <p>Renders the queued error messages of a particual category as a list.
@@ -351,7 +395,27 @@
*/
public String getMsgs(String property)
{
- return StrutsUtils.errorMarkup(property, request, session, application);
+ return getMsgs(property, null);
+ }
+
+
+ /**
+ * <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
+ * @param bundle the message resource bundle to use
+ *
+ * @return The formatted error messages. If no error messages are queued,
+ * an empty string is returned.
+ */
+ public String getMsgs(String property, String bundle)
+ {
+ return StrutsUtils.errorMarkup(property, bundle, request,
+ session, application);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org