net.sourceforge.stripes.validation
Class LocalizableError

java.lang.Object
  extended by net.sourceforge.stripes.action.SimpleMessage
      extended by net.sourceforge.stripes.validation.SimpleError
          extended by net.sourceforge.stripes.validation.LocalizableError
All Implemented Interfaces:
Serializable, Message, ValidationError
Direct Known Subclasses:
ScopedLocalizableError

public class LocalizableError
extends SimpleError

Provides a mechanism for creating localizable error messages for presentation to the user. Uses ResourceBundles to provide the localization of the error message. Messages may contain one or more "replacement parameters ". Two replacement parameters are provided by default, they are the field name and field value, and are indices 0 and 1 respectively. To use replacement parameters a message must contain the replacement token {#} where # is the numeric index of the replacement parameter.

For example, to construct an error message with one additional replacement parameter which is the action the user was trying to perform, you might have a properties file entry like:

/action/MyAction.myErrorMessage={1} is not a valid {0} when trying to {2}

At runtime this might get replaced out to result in an error message for the user that looks like "Fixed is not a valid status when trying to create a new bug".

First looks for a resource with the action bean FQN prepended to the supplied message key. If If that cannot be found then looks with the action path as a prefix instead of the FQN. Failing that, the last attempt looks for a resource with the exact message key provided. This allows developers to segregate their error messages by action without having to repeat the action path in the ActionBean. For example a message constructed with new LocalizableError("insufficientBalance") might look for message resources with the following keys:

One last point of interest is where the user friendly field name comes from. Firstly an attempt is made to look up the localized name in the applicable resource bundle using the String beanClassFQN.fieldName where beanClassFQN is the fully qualified name of the bean class, and fieldName is the name of the field on the form. The second attempt is made with String actionPath.fieldName where actionPath is the action of the form in the JSP (or equally, the path given in the @UrlBinding annotation in the ActionBean class). Finally, the last attempt uses fieldName by itself.

See Also:
MessageFormat, ResourceBundle, Serialized Form

Constructor Summary
LocalizableError(String messageKey, Object... parameter)
          Creates a new LocalizableError with the message key provided, and optionally zero or more replacement parameters to use in the message.
 
Method Summary
 boolean equals(Object o)
          Generated equals method that compares each field and super.equals().
 String getMessageKey()
           
protected  String getMessageTemplate(Locale locale)
          Method responsible for using the information supplied to the error object to find a message template.
 int hashCode()
          Generated hashCode method.
 
Methods inherited from class net.sourceforge.stripes.validation.SimpleError
getActionPath, getBeanclass, getFieldName, getFieldValue, getMessage, resolveFieldName, setActionPath, setBeanclass, setFieldName, setFieldValue
 
Methods inherited from class net.sourceforge.stripes.action.SimpleMessage
getMessage, getReplacementParameters
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LocalizableError

public LocalizableError(String messageKey,
                        Object... parameter)
Creates a new LocalizableError with the message key provided, and optionally zero or more replacement parameters to use in the message. It should be noted that the replacement parameters provided here can be referenced in the error message starting with number 2.

Parameters:
messageKey - a key to lookup a message in the resource bundle
parameter - one or more replacement parameters to insert into the message
Method Detail

getMessageTemplate

protected String getMessageTemplate(Locale locale)
Method responsible for using the information supplied to the error object to find a message template. In this class this is done simply by looking up the resource corresponding to the messageKey supplied in the constructor, first with the FQN prepended, then with the action path prepended and finally bare.

Overrides:
getMessageTemplate in class SimpleMessage
Parameters:
locale - the Locale of the message template desired
Returns:
the message (potentially with TextFormat replacement tokens).

equals

public boolean equals(Object o)
Generated equals method that compares each field and super.equals().

Overrides:
equals in class SimpleError
Parameters:
o - another object that is a SimpleMessage or subclass thereof
Returns:
true if the two objects will generate the same user message, false otherwise

hashCode

public int hashCode()
Generated hashCode method.

Overrides:
hashCode in class SimpleError

getMessageKey

public String getMessageKey()


? Copyright 2005-2006, Stripes Development Team.