4 Replies Latest reply: Jan 15, 2013 7:55 PM by jmsjr RSS

    Allowing EL expression in a resource bundles

    jmsjr
      Mojarra 2.1.7-jbossorg-1 (20120227-1401)
      JBoss 7.1.1

      EL expressions in a resource bundle are currently not evaluated. That is, an an example, I have the following in the requiredMessage attribute of a JSF input control:
      requiredMessage="Status for #{someBean.documentMap[claimDocument.id].description} is required."
      I would like to move this to a messages.properties file like so:
      required.claimDocumentStatus=Status for #{someBean.documentMap[claimDocument.id].description} is required.
      .. so that the requiredMessage attribute becomes:
      requiredMessage="#{msg['required.claimDocumentStatus']}"
      Now the output for h:message(s) is
      Status for #{someBean.documentMap[claimDocument.id].description} is required.
      As I have found out, JSF resource bundles does not resolve EL expressions:

      http://stackoverflow.com/questions/6547136/el-variable-in-jsf-resourcebundle

      What I am trying to do is to allow such resolution / resolving to take place for JSF resource bundles. Now there is a class called "javax.el.ResourceBundleELResolver". I was wondering what it would take to extend this class to allow such a thing to take place. There does not seem to be a way to override it or to specify a custom resolver via faces-config.xml.

      Any thoughts ?

      Edited by: jmsjr on 14-Jan-2013 21:53
        • 1. Re: Allowing EL expression in a resource bundles
          r035198x
          You could create a custom EL resolver function which would have the limitation of not being able to accept var arged methods, http://stackoverflow.com/questions/9280915/how-to-parameterize-requiredmessage-attribute-in-composite-component
          • 2. Re: Allowing EL expression in a resource bundles
            gimbal2
            IMO this requirement, if it is possible to implement, is only going to lead to a hard to maintain application. But I guess that's something you have to experience before you believe it.

            You can't use EL directly in a resource bundle out of the box, but it is possible to parametrize them anyway, did you know that? Perhaps that is already good enough for you.

            http://murygin.wordpress.com/2010/04/23/parameter-substitution-in-resource-bundles/

            http://stackoverflow.com/questions/5697189/pass-parameters-to-messages-from-resource-bundle-to-components-other-than-hou
            • 3. Re: Allowing EL expression in a resource bundles
              jmsjr
              gimbal2 wrote:
              IMO this requirement, if it is possible to implement, is only going to lead to a hard to maintain application. But I guess that's something you have to experience before you believe it.

              You can't use EL directly in a resource bundle out of the box, but it is possible to parametrize them anyway, did you know that? Perhaps that is already good enough for you.

              http://murygin.wordpress.com/2010/04/23/parameter-substitution-in-resource-bundles/

              http://stackoverflow.com/questions/5697189/pass-parameters-to-messages-from-resource-bundle-to-components-other-than-hou
              Yes, I am aware of parameterising the resource bundles. However, these require you to use f:param .. and the last time I tried, these parameters were not available / visible at the requiredMessage attribute. I'll try it again anyway.
              • 4. Re: Allowing EL expression in a resource bundles
                jmsjr
                jmsjr wrote:
                gimbal2 wrote:
                IMO this requirement, if it is possible to implement, is only going to lead to a hard to maintain application. But I guess that's something you have to experience before you believe it.

                You can't use EL directly in a resource bundle out of the box, but it is possible to parametrize them anyway, did you know that? Perhaps that is already good enough for you.

                http://murygin.wordpress.com/2010/04/23/parameter-substitution-in-resource-bundles/

                http://stackoverflow.com/questions/5697189/pass-parameters-to-messages-from-resource-bundle-to-components-other-than-hou
                Yes, I am aware of parameterising the resource bundles. However, these require you to use f:param .. and the last time I tried, these parameters were not available / visible at the requiredMessage attribute. I'll try it again anyway.
                As I have re-discovered, you cannot parameterise the requiredMessage attribute.

                http://stackoverflow.com/questions/9280915/how-to-parameterize-requiredmessage-attribute-in-composite-component

                The other alternative I am looking at, for the cases where the requiredMessage needs to be parameterised, or needs to have dynamic content, is to call a backing method for the requiredMessage attribute, from which I can then do whatever I need, even use EL expressions. I was thinking of this as a last resort.

                On the other hand, when one uses the validator attribute, usually one ends up adding one or multiple FacesMessage in code within the method being called, where the string of the message already comes from a resource bundle. I was trying to have a delineation whereby:

                1) requiredMessage should only use resource bundles, no backing bean method called.
                2) FacesMessages added via the method called by the validator attribute is OK