3

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

ページの例

<h:outputText value="#{bean.methodWithParameters('key.en.currentDate', '2012-01-01', '00:00')}"/>
<h:outputText value="#{bean.methodWithParameters('key.en.currentTime', '12:00')}"/>

豆の例

public String methodWithParameters(String key, Object ... params) {
    String langValue = LanguageBean.translate(key);
    return String.format(langValue, params);
}

プロパティの例

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.
4

1 に答える 1

5

可変長引数は EL ではサポートされていません。

具体的な機能要件に関しては、これは完全に間違っています。JSF で国際化/ローカリゼーションを作り直すのではなく、JSF が提供する機能を使用する必要があります。<resource-bundle>これには infaces-config.xmlまたは<f:loadBundle>in Facelets ファイルを使用する必要があります。これにより、API によってファイルが読み込まれ、ResourceBundleAPI を使用しMessageFormatてメッセージがフォーマットされます。<h:outputFormat>その後、 with で文字列をフォーマットできます<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}.

意見:

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

さらにen、キーのそれが英語を表しているかどうかはわかりませんが、それが実際に言語を表している場合は、別の間違いを犯しています. 個別の言語には、 API ルールに準拠する 、 などの独自のpropertiesファイルが必要です。text_en.propertiestext_de.propertiesResourceBundle

以下も参照してください。

于 2012-03-27T14:39:54.280 に答える