JSF/CDI:使用bean从资源请求字符串
我有一个在会话范围内提供“翻译方法”的简单beanJSF/CDI:使用bean从资源请求字符串,jsf,jsf-2,cdi,Jsf,Jsf 2,Cdi,我有一个在会话范围内提供“翻译方法”的简单bean String getText(String key, Object args ...) { ...// lookup in property resource return value; } 我正试图调用这个bean来获取我的UI组件本地化的文本字符串。尝试调用上述函数时,例如通过 <p:outputLabel for="name" value="#{lang.getText(xyz,arg1)}" /
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:参数数目错误
现在,我的问题是
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?
1)这通常是本地化组件的好选择吗?
2) 因为我可以通过bean1.bean2.myfield来处理嵌套的bean字段
在处理点分隔属性时如何避免冲突,
i、 e.x.y.z而不是xyz?
我认为我目前的思路不对…表达式语言不支持带有varargs的方法表达式。作为解决方法,您可以引入方法
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)}
表达式语言不支持带有varargs的方法表达式。作为解决方法,您可以引入方法
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)}
通常您应该使用
<?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>
以类似的方式@hwellmann建议(+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>
以类似的方式@hwellmann提出了(+1)我也这么认为。通过用户定义的标签调用多参数函数时也会遇到同样的问题。我也这么认为。通过用户定义的标记调用多参数函数时也会出现同样的问题。
@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);
}
}