net.sourceforge.stripes.validation
Class SimpleError

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

public class SimpleError
extends SimpleMessage
implements ValidationError

Validation error message that allows for supplying the error message at the time of creation - i.e. not through a resource bundle or other external mechanism. SimpleError will still attempt to lookup the field name in the field name bundle, but as with other errors, it will fall back to a prettified version of the field name that is used in the input tag.

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 supply a message like:

{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".

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, Serialized Form

Constructor Summary
SimpleError(String message, Object... parameter)
          Constructs a simple error message.
 
Method Summary
 boolean equals(Object o)
          Generated equals method that ensures the message, field, parameters and action path are all equal.
 String getActionPath()
          Provides subclasses access to the name of the form on which the errored field occurs.
 Class<? extends ActionBean> getBeanclass()
          Returns the class of the ActionBean associated to the request.
 String getFieldName()
          Provides subclasses access to the field name.
 String getFieldValue()
          Provides subclasses with access to the value of the field that is in error.
 String getMessage(Locale locale)
          Looks up the field name in the resource bundle (if it exists) so that it can be used in the message, and then defers to it's super class to combine the message template with the replacement parameters provided.
 int hashCode()
          Hash code based on the message, field name key, action path and parameters.
protected  void resolveFieldName(Locale locale)
          Takes the form field name supplied and tries first to resolve a String in the locale specific bundle for the field name, and if that fails, will try to make a semi-friendly name by parsing the form field name.
 void setActionPath(String actionPath)
          Sets the binding path of the ActionBean on which the errored field occurs.
 void setBeanclass(Class<? extends ActionBean> beanclass)
          Sets the class of the ActionBean associated to the request.
 void setFieldName(String name)
          Sets the name of the form field in error.
 void setFieldValue(String value)
          Sets the value of the field that is in error.
 
Methods inherited from class net.sourceforge.stripes.action.SimpleMessage
getMessage, getMessageTemplate, getReplacementParameters
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleError

public SimpleError(String message,
                   Object... parameter)
Constructs a simple error message.

Parameters:
message - the String message (template) to display
parameter - zero or more parameters for replacement into the message
Method Detail

getMessage

public String getMessage(Locale locale)
Looks up the field name in the resource bundle (if it exists) so that it can be used in the message, and then defers to it's super class to combine the message template with the replacement parameters provided.

Specified by:
getMessage in interface Message
Overrides:
getMessage in class SimpleMessage
Parameters:
locale - the locale of the current request
Returns:
String the message stored under the messageKey supplied

resolveFieldName

protected void resolveFieldName(Locale locale)
Takes the form field name supplied and tries first to resolve a String in the locale specific bundle for the field name, and if that fails, will try to make a semi-friendly name by parsing the form field name. The result is stored in replacementParameters[0] for message template parameter replacement.


setFieldName

public void setFieldName(String name)
Sets the name of the form field in error. This is the programmatic name, and hence probably not the name that the user sees.

Specified by:
setFieldName in interface ValidationError

getFieldName

public String getFieldName()
Provides subclasses access to the field name.

Specified by:
getFieldName in interface ValidationError

setFieldValue

public void setFieldValue(String value)
Sets the value of the field that is in error.

Specified by:
setFieldValue in interface ValidationError

getFieldValue

public String getFieldValue()
Provides subclasses with access to the value of the field that is in error.

Specified by:
getFieldValue in interface ValidationError

setActionPath

public void setActionPath(String actionPath)
Sets the binding path of the ActionBean on which the errored field occurs.

Specified by:
setActionPath in interface ValidationError

getActionPath

public String getActionPath()
Provides subclasses access to the name of the form on which the errored field occurs.

Specified by:
getActionPath in interface ValidationError

getBeanclass

public Class<? extends ActionBean> getBeanclass()
Returns the class of the ActionBean associated to the request.

Specified by:
getBeanclass in interface ValidationError

setBeanclass

public void setBeanclass(Class<? extends ActionBean> beanclass)
Sets the class of the ActionBean associated to the request.

Specified by:
setBeanclass in interface ValidationError

equals

public boolean equals(Object o)
Generated equals method that ensures the message, field, parameters and action path are all equal.

Overrides:
equals in class SimpleMessage
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()
Hash code based on the message, field name key, action path and parameters.

Overrides:
hashCode in class SimpleMessage


? Copyright 2005-2006, Stripes Development Team.