jsf自定义所有错误消息

时间:2017-03-29 12:21:45

标签: jsf jboss error-messaging

我的任务是将应用程序的所有消息翻译成另一种不同于英语的语言。棘手的部分是JSF框架本身生成的消息。我遇到了各种文章,展示了如何自定义某些特定的转换或验证错误消息,但我有兴趣自定义应用程序可能生成的每个可能的错误消息(包括,例如,身份验证和导航错误消息)。

是否有包含所有可能错误消息的文件?

到目前为止,我遇到了一个名为 Messages.properties 的文件,该文件位于 jsf-api jar 中,其中包含验证,< em> Converter 和 Component 错误消息。但这还不够,除此之外还有很多其他错误。

这个jar实际命名为 jboss-jsf-api_2.1_spec-2.1.28.Final-redhat-1.jar ,位于 jboss-eap \ modules \ system \ layers下\ base \ javax \ faces \ api \ main 文件夹。
在这个jar中,Messages.properties文件位于 javax \ faces 包下。

我目前使用的是红帽JBoss企业应用平台 - 版本 6.4 .0.GA,JSF实现版本: 2.1 .28.Final-redhat-1

1 个答案:

答案 0 :(得分:0)

问题可以分为两部分:

  1. 如何覆盖框架错误消息?

  2. JSF框架可以生成的所有可能错误消息的列表是什么?

  3. 第1部分:覆盖框架的错误信息

    在我的项目中,在WebContent \ WEB-INF下,有faces-config.xml,其中包含<message-bundle>resources</message-bundle>。 'resources'指向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文件中的条目为指导。< strong> 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。。在类文件编辑器中,我有以下几行:

      // 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
    

    不幸的是,鉴于必须提供错误消息的大量类,我认为这不是一个可行的解决方案。

    <强>更新

    我刚刚意识到为什么只应提供转换和验证错误消息:因为它们与用户输入相关,程序员无法控制。所有其他错误,如导航相关问题,必须由程序员正确处理。这些错误不应该首先出现,所以无论如何翻译这些消息都没有意义。