【问题标题】:JSF/CDI: Using bean to request strings from resourcesJSF/CDI:使用 bean 从资源中请求字符串
【发布时间】:2014-03-12 15:29:19
【问题描述】:

我有一个在会话范围内提供“翻译方法”的简单 bean

String getText(String key, Object args ...) {

    ...// lookup in property resource
    return value;
}

我正在尝试调用此 bean 以使我的 UI 组件本地化文本字符串。当尝试调用上述函数时,例如由

        <p:outputLabel for="name" value="#{lang.getText(xyz,arg1)}" />
        <p:inputText id="name" value="#{formProject.entity.name}"/>
        <p:message for="name" id="msgName" />

我得到一个 java.lang.IllegalArgumentException: wrong number of arguments

现在,我的问题

1) Is this generally a good alternative to <f:loadBundle> to localize my components?
2) Since I am able to address my nested bean fields via bean1.bean2.myfield 
   how to avoid conflicts when adressing the properties dot-sepatated, 
   i.e. x.y.z instead of xyz?

我认为我目前没有走在正确的轨道上......

【问题讨论】:

    标签: jsf jsf-2 cdi


    【解决方案1】:

    一般你应该使用

    <?xml version='1.0' encoding='UTF-8'?>
    <faces-config version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
    
        <application>
            <locale-config>
                <default-locale>it</default-locale>
                <supported-locale>it</supported-locale>
                <supported-locale>en</supported-locale>
                <supported-locale>de</supported-locale>
            </locale-config>
    
            <resource-bundle>
                <base-name>/bundle</base-name>
                <var>bundle</var>
            </resource-bundle>
        </application>
    
    </faces-config>
    

    并通过

    获取标签
    <p:outputLabel for="name" value="#{bundle.foo}" />
    <p:inputText id="name" value="#{formProject.entity.name}"/>
    <p:message for="name" id="msgName" />
    

    但是虽然您可以通过这种方式访问​​带点的名称

    <p:outputLabel for="name" value="#{bundle['foo.bar']}" />
    

    你不能传递参数(如果没有插值器,我不知道怎么做,或者根本不可能)

    混合解决方案可以是

    @ManagedBean
    public class LabelBean
    {
        private String getText(String key, String... args)
        {
            String message = Faces.evaluateExpressionGet("#{bundle['" + key + "']}");
            return MessageFormat.format(message, args);
        }
    
        public String getText1(String key, String arg)
        {
            return getText(key, arg);
        }
    
        public String getText2(String key, String arg1, String arg2)
        {
            return getText(key, arg1, arg2);
        }
    }
    

    以类似的方式@hwellmann 提出 (+1)

    【讨论】:

      【解决方案2】:

      表达式语言不支持带有可变参数的方法表达式。作为一种解决方法,您可以引入方法

      String getText2(String key, Object arg1, Object arg2);
      String getText3(String key, Object arg1, Object arg2, Object arg3);
      

      等等

      关于2),只需使用#{lang.getText('x.y.z',arg1)}

      【讨论】:

      • 我是这么认为的。通过自定义标签调用多参数函数时同样的问题。
      猜你喜欢
      • 2014-04-23
      • 1970-01-01
      • 2013-02-22
      • 2014-02-28
      • 2012-01-20
      • 1970-01-01
      • 1970-01-01
      • 2015-04-15
      • 2015-05-05
      相关资源
      最近更新 更多