您的位置:首页 > Web前端 > JavaScript

JSF教程三 数据转换与验证 提示错误设置为中文

2011-09-01 16:43 996 查看
http://www.4ucode.com/Study/Topic/1309146

错误讯息如果有设定的话,就使用设定值,如果没有设定的话,就使用默认值。

在使用标准转换器或验证器时,当发生错误时,会有一些预设的错误讯息显示,这些讯息可以使用<h:messages>或<h:message>卷标来显示出来,而这些预设的错误讯息也是可以修改的,您所要作的是提供一个讯息资源文件,例如:
messages.properties
javax.faces.component.UIInput.CONVERSION=Format Error.
javax.faces.component.UIInput.REQUIRED=Please input your data.
javax.faces.validator.LongRangeValidator.NOT_IN_RANGE=验证错误:输入值必须在{0}和{1}之间
....
  javax.faces.component.UIInput.CONVERSION是用来设定当转换器发现错误时显示的讯息,而javax.faces.component.UIInput.REQUIRED是在标签设定了required为true,而使用者没有在字段输入时显示的错误讯息。
  您要在faces-config.xml中告诉JSF您使用的讯息文件名称,例如:
faces-config.xml
<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
"http://java.sun.com/dtd/web-facesconfig_1_0.dtd">

<faces-config>
<application>
<local-config>
<default-locale>en</default-locale>
<supported-locale>zh_TW</supported-locale>
</local-config>
<message-bundle>messages</message-bundle>
</application>
.....

</faces-config>
  在这边我们设定了讯息档案的名称为messages_xx_YY.properties,其中xx_YY是根据您的Locale来决定,转换器或验证器的错误讯息如果有设定的话,就使用设定值,如果没有设定的话,就使用默认值。
  验证器错误讯息,除了上面的javax.faces.component.UIInput.REQUIRED之外,还有以下的几个:
讯息识别
预设讯息
用于
javax.faces.validator.NOT_IN_RANGE
Validation Error: Specified attribute is not between the expected values of {0} and {1}.
DoubleRangeValidator与LongRangeValidator,{0}与{1}分别代表minimum与maximum所设定的属性
javax.faces.validator.DoubleRangeValidator.MAXIMUM、javax.faces.validator.LongRangeValidator.MAXIMUM
Validation Error: Value is greater than allowable maximum of '{0}'.
DoubleRangeValidator或LongRangeValidator,{0}表示maximum属性
javax.faces.validator.DoubleRangeValidator.MINIMUM、javax.faces.validator.LongRangeValidator.MINIMUM
Validation Error: Value is less than allowable minimum of '{0}'.
DoubleRangeValidator或LongRangeValidator,{0}代表minimum属性
javax.faces.validator.DoubleRangeValidator.TYPE、javax.faces.validator.LongRangeValidator.TYPE
Validation Error: Value is not of the correct type.
DoubleRangeValidator或LongRangeValidator
javax.faces.validator.LengthValidator.MAXIMUM
Validation Error: Value is greater than allowable maximum of ''{0}''.
LengthValidator,{0}代表maximum
javax.faces.validator.LengthValidator.MINIMUM
Validation Error: Value is less than allowable minimum of ''{0}''.
LengthValidator,{0}代表minimum属性
  在您提供自订讯息的时候,也可以提供{0}或{1}来设定显示相对的属性值,以提供详细正确的错误提示讯息。
  讯息的显示有概述讯息与详述讯息,如果是详述讯息,则在识别上加上 "_detail",例如:
javax.faces.component.UIInput.CONVERSION=Error.
javax.faces.component.UIInput.CONVERSION_detail= Detail Error.
....
  除了在讯息资源文件中提供讯息,您也可以在程序中使用FacesMessage来提供讯息,例如在 自订验证器 中我们就这么用过:
....
if(password.length() < 6) {
FacesMessage message = new FacesMessage(


参考:csdn空间5\JSF-API-1.2.chm\tlddocs标签

csdn空间5\jsf_src.zip的javax\faces\Messages.properties中有,把所有的英文写成中文就可以了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: