- Java EE 8 Application Development
- David R. Heffelfinger
- 577字
- 2021-07-02 22:05:02
Customizing message text
Sometimes it is desirable to override the text of JSF's default validation errors. Default validation errors are defined in a resource bundle called Messages.properties. This file can typically be found inside one of the JSF JAR Files included with your application server. For example, GlassFish includes it inside a JAR file called javax.faces.jar file under [glassfish installation directory]/glassfish/modules. The file contains several messages; we are only interested in validation errors at this point. The default validation error messages are defined as follows:
javax.faces.validator.DoubleRangeValidator.MAXIMUM={1}: Validation Error: Value is greater than allowable maximum of "{0}" javax.faces.validator.DoubleRangeValidator.MINIMUM={1}: Validation Error: Value is less than allowable minimum of ''{0}'' javax.faces.validator.DoubleRangeValidator.NOT_IN_RANGE={2}: Validation Error: Specified attribute is not between the expected values of {0} and {1}. javax.faces.validator.DoubleRangeValidator.TYPE={0}: Validation Error: Value is not of the correct type javax.faces.validator.LengthValidator.MAXIMUM={1}: Validation Error: Length is greater than allowable maximum of ''{0}'' javax.faces.validator.LengthValidator.MINIMUM={1}: Validation Error: Length is less than allowable minimum of ''{0}'' javax.faces.validator.LongRangeValidator.MAXIMUM={1}: Validation Error: Value is greater than allowable maximum of ''{0}'' javax.faces.validator.LongRangeValidator.MINIMUM={1}: Validation Error: Value is less than allowable minimum of ''{0}'' javax.faces.validator.LongRangeValidator.NOT_IN_RANGE={2}: Validation Error: Specified attribute is not between the expected values of {0} and {1}. javax.faces.validator.LongRangeValidator.TYPE={0}: Validation Error: Value is not of the correct type. javax.faces.validator.NOT_IN_RANGE=Validation Error: Specified attribute is not between the expected values of {0} and {1}. javax.faces.validator.RegexValidator.PATTERN_NOT_SET=Regex pattern must be set. javax.faces.validator.RegexValidator.PATTERN_NOT_SET_detail=Regex pattern must be set to non-empty value. javax.faces.validator.RegexValidator.NOT_MATCHED=Regex Pattern not matched javax.faces.validator.RegexValidator.NOT_MATCHED_detail=Regex pattern of ''{0}'' not matched javax.faces.validator.RegexValidator.MATCH_EXCEPTION=Error in regular expression. javax.faces.validator.RegexValidator.MATCH_EXCEPTION_detail=Error in regular expression, ''{0}'' javax.faces.validator.BeanValidator.MESSAGE={0}
In order to override the default error messages, we need to create our own resource bundle, using the same keys used in the default one, but altering the values to suit our needs. Here is a very simple customized resource bundle for our application. For example, to overwrite the message for minimum length validation, we would add the following property to our custom resource bundle:
javax.faces.validator.LengthValidator.MINIMUM={1}: minimum allowed length is ''{0}''
In this resource bundle, we override the error message for when the value entered for a field validated by the <f:validateLength> tag is less than the allowed minimum. In order to let our application know that we have a custom resource bundle for message properties, we need to modify the application's faces-config.xml file:
<?xml version='1.0' encoding='UTF-8'?> <faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"> <application>
<message-bundle>net.ensode.Messages</message-bundle>
</application> </faces-config>
As we can see, the only thing we need to do to the application's faces-config.xml file is to add a <message-bundle> element indicating the name and location of the resource bundle containing our custom messages.
Custom error message text definitions are one of the few cases where we still need to define a faces-config.xml file for modern JSF applications. However, notice how simple our faces-config.xml file is, a far cry from a typical faces-config.xml for JSF 1.x, which typically contains named bean definitions, navigation rules, and JSF validator definitions.
After adding our custom message resource bundle and modifying the application's
faces-config.xml file, we can see our custom validation message in action:
As can be seen in the screenshot, if we haven't overridden a validation message, the default will still be displayed. In our resource bundle we only overrode the minimum length validation error message, therefore our custom error message is shown in the First Name text field. Since we didn't override the error message in the other standard JSF validators, the default error message is shown for each one of them. The email validator is the custom validator we developed previously in this chapter; since it is a custom validator, its error message is not affected.
- Dynamics 365 for Finance and Operations Development Cookbook(Fourth Edition)
- Java應用與實戰
- JIRA 7 Administration Cookbook(Second Edition)
- Koa開發:入門、進階與實戰
- STM32F0實戰:基于HAL庫開發
- Symfony2 Essentials
- Extending Puppet(Second Edition)
- Linux C編程:一站式學習
- R用戶Python學習指南:數據科學方法
- 深度學習原理與PyTorch實戰(第2版)
- Getting Started with Python
- Scala編程實戰
- ASP.NET 4.0 Web程序設計
- 從零開始學UI:概念解析、實戰提高、突破規則
- Python Social Media Analytics