官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 迁安市| 定日县| 云南省| 霍城县| 抚州市| 迁西县| 高台县| 平果县| 南城县| 新丰县| 化德县| 保山市| 宿松县| 三原县| 永春县| 那曲县| 来宾市| 柏乡县| 武城县| 巴彦淖尔市| 阿克陶县| 富锦市| 泰宁县| 澄城县| 礼泉县| 安仁县| 安康市| 潼南县| 祁东县| 永德县| 冷水江市| 东明县| 无棣县| 松原市| 元阳县| 庆云县| 昌平区| 扶风县| 永城市| 怀仁县| 河间市|