JSF: Make your own error messages.
November 18th, 2008 at 6:14pm |
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:
- Make a file called messages.properties and put it somewhere in your source folder (for example in a resource folder).
- Go to the faces-context.xml, and add the following inside the faces-context tag:
<application>
<message-bundle>resource.messages</message-bundle>
</application> - 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.