在使用标准转换器或验证器时,当发生错误时,会有一些预设的错误讯息显示,这些讯息可以使用<h: messages>或<h:message>标签来显示出来,而这些预设的错误讯息也是可以修改的,您所要作的是提供一个讯息资源档案,例如:
- messages.properties
javax.faces.component.UIInput.CONVERSION=Format Error.
javax.faces.component.UIInput.REQUIRED=Please input your data.
....
javax.faces.component.UIInput.CONVERSION是用来设定当转换器发现错误时显示的讯息,而 javax.faces.component.UIInput.REQUIRED是在标签设定了required为true,而使用者没有在栏位输入时显示的错误讯息。
在这边我们设定了讯息档案的名称为 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} 来设定显示相对的属性值,以提供详细正确的错误提示讯息。
补充:
可以在jsf-api包中的Message_xx.properties中,
找到相应validator或converter的key值
参考代码
- register . jsp
<%@ page language = "java" contentType = "text/html; charset=ISO-8859-1" pageEncoding = "ISO-8859-1" %> <%@ taglib prefix = "f" uri = "http://java.sun.com/jsf/core" %> <%@ taglib prefix = "h" uri = "http://java.sun.com/jsf/html" %> <! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > < html > < head > < meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" > < title > Insert title here </ title > < style > .error { color : red ; font-style : italic ; } </ style > </ head > < body > < f:view > < f:loadBundle var = "msgs" basename = "messages" /> < h:outputText value = "#{msgs.DateTime}" ></ h:outputText > < h:messages ></ h:messages > < h:form > < h:panelGrid columns = "3" > User Name : < h:inputText id = "name" value = "#{user_validator_message.name}" required = "true" label = "userName" > </ h:inputText > < h:message for = "name" styleClass = "error" ></ h:message >
Password : < h:inputSecret id = "password" value = "#{user_validator_message.password}" required = "true" label = "password" > < f:validateLength minimum = "4" ></ f:validateLength > </ h:inputSecret > < h:message for = "password" styleClass = "error" />
Age : < h:inputText id = "age" value = "#{user_validator_message.age}" size = "3" label = "age" required = "true" > < f:validateLongRange minimum = "1" maximum = "100" ></ f:validateLongRange > </ h:inputText > < h:message for = "age" styleClass = "error" />
Amount : < h:inputText id = "amount" value = "#{user_validator_message.amount}" label = "amount" required = "true" > < f:validateDoubleRange minimum = "100" maximum = "20000" ></ f:validateDoubleRange > </ h:inputText > < h:message for = "amount" styleClass = "error" ></ h:message >
Birthday : < h:inputText id = "birthday" value = "#{user_validator_message.birthday}" label = "birthday" required = "true" > < f:converter converterId = "javax.faces.DateTime" /> </ h:inputText > < h:message for = "birthday" ></ h:message > < h:commandButton value = "register" action = "register" ></ h:commandButton > </ h:panelGrid >
</ h:form > </ f:view > </ body > </ html > |
· faces-config-validator-messages.xml
<? xml version = "1.0" encoding = "UTF-8" ?> < faces-config xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:xi = "http://www.w3.org/2001/XInclude" xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd" version = "1.2" > < application > < locale-config > < default-locale > en </ default-locale > < supported-locale > zh_CN </ supported-locale > </ locale-config > < message-bundle > messages </ message-bundle > </ application > < navigation-rule > < from-view-id > /pages/standard_validator_message/register.jsp </ from-view-id > < navigation-case > < from-outcome > register </ from-outcome > < to-view-id > /pages/standard_validator_message/welcome.jsp </ to-view-id > < redirect /> </ navigation-case > </ navigation-rule >
< managed-bean > < managed-bean-name > user_validator_message </ managed-bean-name > < managed-bean-class > com .jxhuang .validator .standard_message.bean.User </ managed-bean-class > < managed-bean-scope > session </ managed-bean-scope > </ managed-bean > </ faces-config > |
· messages.properties
javax.faces.component.UIInput.CONVERSION= Format Error. javax.faces.component.UIInput.REQUIRED= Please input {0} data. javax.faces.validator.LengthValidator.MINIMUM = {1} length is supposed longer than {0} . javax.faces.validator.LongRangeValidator.NOT_IN_RANGE = {2} the range is {0} to {1} . javax.faces.validator.DoubleRangeValidator.NOT_IN_RANGE= {2} the range is {0} to {1} . DateTime= is against the pattern. javax.faces.DateTime= is against the pattern. |
|