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

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.

主站蜘蛛池模板: 普宁市| 榆中县| 阿城市| 松溪县| 荣成市| 综艺| 宜川县| 五原县| 广水市| 维西| 郯城县| 山东| 翁牛特旗| 柏乡县| 天台县| 海丰县| 车险| 伊宁市| 大渡口区| 九龙县| 海阳市| 莎车县| 河北区| 洞口县| 安阳县| 闽清县| 泾川县| 临夏县| 中江县| 铜川市| 改则县| 辉县市| 涡阳县| 阳曲县| 微博| 茶陵县| 西丰县| 临沧市| 常宁市| 科技| 遵义市|