Woo*_*off 3 java el optional-parameters optional-arguments jsf-2
任何想法如何(如果可能)用JSF页面中的可选参数调用java方法?我使用Java 7,JSF 2.1,EL 2.2(Glassfish 3.1.2).提前致谢...
我得到了这个例外
javax.el.ELException: /example.xhtml: wrong number of arguments
Caused by: java.lang.IllegalArgumentException: wrong number of arguments
Run Code Online (Sandbox Code Playgroud)
页面示例
<h:outputText value="#{bean.methodWithParameters('key.en.currentDate', '2012-01-01', '00:00')}"/>
<h:outputText value="#{bean.methodWithParameters('key.en.currentTime', '12:00')}"/>
Run Code Online (Sandbox Code Playgroud)
豆的例子
public String methodWithParameters(String key, Object ... params) {
String langValue = LanguageBean.translate(key);
return String.format(langValue, params);
}
Run Code Online (Sandbox Code Playgroud)
属性示例
key.en.currentDate=Today is %s and current time is %s.
key.en.currentTime=Current time is %s.
key.en.currentDate=Today is %1$s and current time is %2$s.
key.en.currentTime=Current time is %2$s.
Run Code Online (Sandbox Code Playgroud)
EL不支持Varargs.
至于你的具体功能要求,你接近这完全错了.您不应该在JSF中重新发明国际化/本地化,而是使用JSF提供的工具.您应该在Facelets文件<resource-bundle>中faces-config.xml或<f:loadBundle>在Facelets文件中使用它.这将通过ResourceBundleAPI 加载文件并使用MessageFormatAPI格式化消息.然后,您可以通过格式化字符串<h:outputFormat>用<f:param>.
例如 com/example/i18n/text.properties
key.en.currentDate=Today is {0} and current time is {1}.
key.en.currentTime=Current time is {0}.
Run Code Online (Sandbox Code Playgroud)
视图:
<f:loadBundle baseName="com.example.i18n.text" var="text" />
<h:outputFormat value="#{text['key.en.currentDate']}">
<f:param value="2012-01-01" />
<f:param value="00:00" />
</h:outputFormat>
Run Code Online (Sandbox Code Playgroud)
此外,我不确定en键中是否代表英语,但如果它确实代表语言,那么你又犯了一个错误.单独的语言需要有各自己的properties文件一样text_en.properties,text_de.properties等符合ResourceBundleAPI规则.
| 归档时间: |
|
| 查看次数: |
1514 次 |
| 最近记录: |