问题可以分为两部分:
如何覆盖框架错误消息?
JSF 框架可以生成的所有可能的错误消息列表是什么?
第 1 部分:覆盖框架的错误消息
在我的项目中,在WebContent\WEB-INF 下,有faces-config.xml,其中包含<message-bundle>resources</message-bundle>。 “资源”指向 src\resources.properties。在这个 .properties 文件中,我只需要添加相应的条目,比如
javax.faces.converter.DateTimeConverter.DATE={2}: ''{0}'' non poteva essere inteso come una data.
如果应用程序产生此类错误,将显示翻译后的消息。
但是,正如我从Cannot override validation error message 了解到的那样,在 src 下使用 resources.properties 文件是特定于 maven 的。不同的封装技术可能需要不同的解决方案。
第 2 部分:所有错误消息列表
查看 javax.faces.jar http://www.java2s.com/Code/Jar/j/Downloadjavaxfacesjar.htm 中包含的文件,我看到以下包:
javax.faces.application
javax.faces.bean
javax.faces.component
javax.faces.context
javax.faces.convert
javax.faces.el
javax.faces.event
javax.faces.lifecycle
javax.faces.model
javax.faces.render
javax.faces.validator
javax.faces.view
javax.faces.webapp
对于每个包中的每个类,我必须在 resources.properties 中为此类可能引发的每个错误添加条目。
但是一个类可能引发的错误是什么? resource.properties 中的条目应该是什么样的?
我让自己以我在 jboss-jsf-api_2.1_spec-2.1.28.Final-redhat-1 内的 Messages.properties 文件中找到的条目为指导。jar (我在问题中提到过)。它具有以下条目,其中包括:
# ==============================================================================
# Component Errors
# ==============================================================================
javax.faces.component.UIInput.CONVERSION={0}: Conversion error occurred.
javax.faces.component.UIInput.REQUIRED={0}: Validation Error: Value is required.
javax.faces.component.UIInput.UPDATE={0}: An error occurred when processing your submitted information.
在我的 IDE (eclipse) 中,我单击导入语句 import javax.faces.component.UIInput; 以导航到 UIInput.class 。在类文件编辑器中,我有以下几行:
// Field descriptor #193 Ljava/lang/String;
public static final java.lang.String CONVERSION_MESSAGE_ID = "javax.faces.component.UIInput.CONVERSION";
// Field descriptor #193 Ljava/lang/String;
public static final java.lang.String REQUIRED_MESSAGE_ID = "javax.faces.component.UIInput.REQUIRED";
// Field descriptor #193 Ljava/lang/String;
public static final java.lang.String UPDATE_MESSAGE_ID = "javax.faces.component.UIInput.UPDATE";
我注意到String字段的值正是必须在resources.properties中添加的键,例如javax.faces.component.UIInput.CONVERSION。
对于一个新类,例如 javax.faces.validator.RegexValidator 类,我将执行以下操作:在类文件编辑器中打开它,选择名称以 _ID 结尾的那些字段,例如:
// Field descriptor #30 Ljava/lang/String;
public static final java.lang.String VALIDATOR_ID = "javax.faces.RegularExpression";
// Field descriptor #30 Ljava/lang/String;
public static final java.lang.String PATTERN_NOT_SET_MESSAGE_ID = "javax.faces.validator.RegexValidator.PATTERN_NOT_SET";
// Field descriptor #30 Ljava/lang/String;
public static final java.lang.String NOT_MATCHED_MESSAGE_ID = "javax.faces.validator.RegexValidator.NOT_MATCHED";
// Field descriptor #30 Ljava/lang/String;
public static final java.lang.String MATCH_EXCEPTION_MESSAGE_ID = "javax.faces.validator.RegexValidator.MATCH_EXCEPTION";
并将以下条目添加到 resources.properties 文件:
javax.faces.RegularExpression=custom message
javax.faces.validator.RegexValidator.PATTERN_NOT_SET=custom message
javax.faces.validator.RegexValidator.NOT_MATCHED=custom message
javax.faces.validator.RegexValidator.MATCH_EXCEPTION=custom message
不幸的是,鉴于必须提供大量错误消息的类,我认为这不是一个可行的解决方案。
更新
我刚刚意识到为什么只应该提供转换和验证错误消息:因为它们与用户输入有关,程序员无法控制。程序员必须正确处理所有其他错误,如导航相关问题。此类错误首先不应出现,因此无论如何翻译这些消息是没有意义的。