资料来源: PmWiki@caterpillar
作者: caterpillar
在使用标准转换器或验证器时,当发生错误时,会有一些预设的错误讯息显示,这些讯息可以使用<h:messages>或<h:message>卷标来显示出来,而这些预设的错误讯息也是可以修改的,您所要作的是提供一个讯息资源文件,例如:
messages.properties
| javax.faces.component.UIInput.CONVERSION=Format Error. javax.faces.component.UIInput.REQUIRED=Please input your data. .... |
您要在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> |
验证器错误讯息,除了上面的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属性 |
讯息的显示有概述讯息与详述讯息,如果是详述讯息,则在识别上加上 "_detail",例如:
| javax.faces.component.UIInput.CONVERSION=Error. javax.faces.component.UIInput.CONVERSION_detail= Detail Error. .... |
| .... if(password.length() < 6) { FacesMessage message = new FacesMessage( FacesMessage.SEVERITY_ERROR, "字符长度小于6", "字符长度不得小于6"); throw new ValidatorException(message); } .... |
| onlyfun.caterpillar.message1=This is message1. onlyfun.caterpillar.message2=This is message2 with \{0} and \{1}. |
| package onlyfun.caterpillar; import java.util.Locale; import java.util.ResourceBundle; import javax.faces.context.FacesContext; improt javax.faces.component.UIComponent; import javax.faces.application.Application; import javax.faces.application.FacesMessage; .... public void xxxMethod(FacesContext context, UIComponent component, Object obj) { // 取得应用程序代表对象 Application application = context.getApplication(); // 取得讯息档案主名称 String messageFileName = application.getMessageBundle(); // 取得当前 Locale 对象 Locale locale = context.getViewRoot().getLocale(); // 取得讯息绑定 ResourceBundle 对象 ResourceBundle rsBundle = ResourceBundle.getBundle(messageFileName, locale); String message = rsBundle.getString( "onlyfun.caterpillar.message1"); FacesMessage facesMessage = new FacesMessage( FacesMessage.SEVERITY_FATAL, message, message); .... } .... .... |
如果需要在讯息资源文件中设定{0}、{1}等参数,则可以如下:
| .... String message = rsBundle.getString( "onlyfun.caterpillar.message2"); Object[] params = {"param1", "param2"}; message = java.text.MessageFormat.format(message, params); FacesMessage facesMessage = new FacesMessage( FacesMessage.SEVERITY_FATAL, message, message); .... |
