标准的JSF转换和验证错误消息过于详细,无论是技术性的还是某个时候的,都不是真正的人类可读性。 在本文中,它将向您展示如何在JSF 2.0中自定义标准转换或验证错误消息。
摘要指南
- 从jsf-api-2.x.jar文件“ Messages.properties”中找到您的消息密钥。
- 创建自己的属性文件,并在“ Messages.properties”文件中放置与您相同的消息密钥,并用自定义错误消息覆盖它。
- 在“ faces-config.xml”中注册属性文件,将其作为应用程序级别。
- 做完了
1. Messages.properties
所有JSF标准转换和验证错误消息均存储在“ Messages.properties”文件中,该文件可从jsf-api-2.x.jar ,“ javax \ faces \ Messages.properties ”中找到,请参见下图:
请参阅此“ Messages.properties ”文件的一部分
...
# ===================================================================
# Converter Errors
# ===================================================================
javax.faces.converter.DateTimeConverter.DATE={2}: ''{0}'' could not be understood as a date.
javax.faces.converter.DateTimeConverter.DATE_detail={2}: ''{0}'' could not be understood as a date. Example: {1}
...
# ====================================================================
# Validator Errors
# ====================================================================
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}''
...
例如,
1. <f:validateLength最小值=“ 5”最大值=“ 10” />
如果最大长度验证失败,则JSF将获得“ javax.faces.validator.LengthValidator.MAXIMUM”。
如果最小长度验证失败,则JSF将获得“ javax.faces.validator.LengthValidator.MINIMUM”。
2. <f:convertDateTime pattern =“ dM-yyyy” />
如果日期验证失败,则JSF将获得“ javax.faces.converter.DateTimeConverter.DATE_detail”。
注意
如果不确定哪个键与哪个验证者标签匹配,只需显示一次错误消息,然后将其与“ Messages.properties”进行比较,即可知道要覆盖哪个键。
2.自定义错误消息
创建一个名为“ MyMessage.properties ”的属性文件(可以是您喜欢的任何名称),然后将消息密钥和自定义错误消息放入其中。 以后,将此属性文件放入项目资源文件夹。
MyMessage.properties
javax.faces.converter.DateTimeConverter.DATE={2}: ''{0}'' could not be understood as a date.
javax.faces.converter.DateTimeConverter.DATE_detail=Invalid date format.
javax.faces.validator.LengthValidator.MINIMUM=Minimum length of ''{0}'' is required.
现在,你要自定义为“javax.faces.converter.DateTimeConverter.DATE_detail““javax.faces.validator.LengthValidator.MINIMUM”和转换错误消息验证错误消息。
注意
对于XXX_detail消息密钥,您还必须覆盖其父密钥(摘要消息),即不带“ _detail”的XXX。 否则,JSF将忽略您的新自定义错误消息,并继续从“ Messages.properties”获取标准错误消息,这可能是JSF 2.0中的错误吗?
3.注册消息包
在“ faces-config.xml”中注册您的自定义属性文件,将其作为应用程序级别。
faces-config.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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<application>
<message-bundle>
com.mkyong.MyMessage
</message-bundle>
</application>
</faces-config>
4.演示
在JSF页面中,添加<f:validateLength />和<f:convertDateTime />验证。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
>
<h:body>
<h1>Customize validation error message in JSF 2.0</h1>
<h:form>
<h:panelGrid columns="3">
Enter your username :
<h:inputText id="username" value="#{user.username}"
size="20" required="true" label="Username">
<f:validateLength minimum="5" maximum="10" />
</h:inputText>
<h:message for="username" style="color:red" />
Enter your DOB :
<h:inputText id="dob" value="#{user.dob}"
size="20" required="true" label="Date of Birth">
<f:convertDateTime />
</h:inputText>
<h:message for="dob" style="color:red" />
</h:panelGrid>
<h:commandButton value="Submit" action="result" />
</h:form>
</h:body>
</html>
如果验证失败,请立即显示您的自定义错误消息。
下载源代码
下载它– JSF-2-Custom-Validation-Error-Message-Example.zip (11KB)
参考
翻译自: https://mkyong.com/jsf2/customize-validation-error-message-in-jsf-2-0/