Life of the IT-Drummer

A web log from Tobias Rusås Olsen.

JSF: Make your own error messages.

Today we had trouble with changing the default error messages in JSF, they were pretty confusing. After surfing the net, we found an easy solution:

  1. Make a file called messages.properties and put it somewhere in your source folder (for example in a resource folder).
  2. Go to the faces-context.xml, and add the following inside the faces-context tag:
    <application>
    <message-bundle>resource.messages</message-bundle>
    </application>
  3. Open the messages.properties and add lines using this format: javax.faces.component.UIInput.REQUIRED=New value

These are some of the replaceable fields:

Key Description
javax.faces.component.UIInput.CONVERSION Conversion error occurred
javax.faces.component.UIInput.REQUIRED Value is required
javax.faces.component.UISelectOne.INVALID Value is not a a valid option
javax.faces.component.UISelectMany.INVALID Value is not a valid option
javax.faces.validator.NOT_IN_RANGE Specified attribute is not between the expected values of {0} and {1}
javax.faces.validator.DoubleRangeValidator.MAXIMUM Value is greater than allowable maximum of ‘{0}’
javax.faces.validator.DoubleRangeValidator.MINIMUM Value is less than allowable minimum of ‘{0}’
javax.faces.validator.DoubleRangeValidator.TYPE Value is not of the correct type
javax.faces.validator.LengthValidator.MAXIMUM Value is greater than allowable maximum of ‘{0}’
javax.faces.validator.LengthValidator.MINIMUM Value is less than allowable minimum of ‘{0}’
javax.faces.validator.LongRangeValidator.MAXIMUM Value is greater than allowable maximum of ‘{0}’
javax.faces.validator.LongRangeValidator.MINIMUM Value is less than allowable minimum of ‘{0}’
javax.faces.validator.LongRangeValidator.TYPE Value is not of the correct type

If you have a required field, you already have a value overwritten for javax.faces.component.UIInput.REQUIRED, but it’s not very nice, therefore you might wanna replace it with something smoother.

RSS 2.0 | Trackback | Comment

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="">