如何读取消息并将参数传递给JSF 2.0中的message.properties文件

Bas*_*sit 26 jsf-2

假设我有一个messages.properties文件

windowTitle=Accessing Form Elements with JavaScript
namePrompt=Name:
passwordPrompt=Password:
confirmPasswordPrompt=Confirm Password:
Run Code Online (Sandbox Code Playgroud)

我在faces-config.xml中有这样的条目

<faces-config version="2.0"
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">

    <application>
        <resource-bundle>
            <base-name>pk.mazars.basitMahmood.messages</base-name>
            <var>msgs</var>
        </resource-bundle>
    </application>

</faces-config>
Run Code Online (Sandbox Code Playgroud)

在我的xhtml页面上,我可以像这样访问它

<h:panelGrid columns="2" columnClasses="evenColumns, oddColumns">
    #{msgs.namePrompt}
    <h:inputText/>
    #{msgs.passwordPrompt}
    <h:inputSecret id="password"/>
    #{msgs.confirmPasswordPrompt}
    <h:inputSecret id="passwordConfirm"/>
</h:panelGrid>
Run Code Online (Sandbox Code Playgroud)

但是如何从Java中读取此文件.比如,假设我必须打印这样的消息,或者提示用户必须输入名称

System.out.println(msgs.namePrompt + "must be entered")
Run Code Online (Sandbox Code Playgroud)

如何从我的java代码中读取msgs.namePrompt值.

还假设我的消息文件中有这样的条目

sure=Are you sure, you want to delete the <Field>?
remove=Are you sure you want to remove the<Field> and <Field>?
close=Are you sure you want to mark the <Field> as Closed?
created=<Field> is successfully created
updated=<Field> is successfully updated
Run Code Online (Sandbox Code Playgroud)

是否有任何技术可以将参数传递给我的messages.properties文件.就像我想在我的java代码中做这样的事情

System.out.println(msgs.sure("Name"));   //<Field> is replace with Name
System.out.println(msgs.remove("Age", "Gender"));  //  First Field replace by Age, and second is replace by Gender
Run Code Online (Sandbox Code Playgroud)

谢谢.

Mat*_*ndy 61

facelets中的参数化资源字符串:

本教程中所述,您可以使用h:outputFormatf:param替换资源包字符串中的参数:

<h:outputFormat value="#{msg['message.param1']}">
   <f:param value="param0" />
</h:outputFormat>
<h:outputFormat value="#{msg['message.param2']}">
   <f:param value="param0" />
   <f:param value="param1" />
</h:outputFormat>

//properties file
message.param1 = This is "message.param1" - {0}
message.param2 = This is "message.param2" - {0} and {1}
Run Code Online (Sandbox Code Playgroud)

在Java中,您可以访问如下属性文件:

import java.util.ResourceBundle;
...
ResourceBundle rb = ResourceBundle.getBundle("pk.mazars.basitMahmood.messages");
Run Code Online (Sandbox Code Playgroud)

可以使用javax.text.MessageFormat类处理参数化属性:

MessageFormat.format(rb.getString(key), params);
Run Code Online (Sandbox Code Playgroud)

如果您正在使用不同的语言环境以及参数化和非参数化属性,则可以使用如下的简短帮助方法:

public static String getMessageResourceString(String bundleName, String key, Object params[], Locale locale) {

        String text;
        ResourceBundle bundle = ResourceBundle.getBundle(bundleName, locale);

        try {
            text = bundle.getString(key);
        } catch (MissingResourceException e) {
            text = "?? key " + key + " not found ??";
        }

        if (params != null) {
            MessageFormat mf = new MessageFormat(text, locale);
            text = mf.format(params, new StringBuffer(), null).toString();
        }

        return text;
    }
Run Code Online (Sandbox Code Playgroud)