Java 如何封装参数化消息的逻辑?

Java 如何封装参数化消息的逻辑?,java,jakarta-ee,properties,jstl,message,Java,Jakarta Ee,Properties,Jstl,Message,我正在使用java.util.resourcebundle格式化JSTL消息,这很好: 我使用你可以在这里看到的类MessageFormat。现在我想把它封装到一个方法中,这个方法就是getParameterizedMessage(String键,String[]parameters),但我不知道怎么做。现在,只显示一两条带有参数的消息需要做大量工作: UserMessage um = null; ResourceBundle messages = ResourceBundle.getBu

我正在使用java.util.resourcebundle格式化JSTL消息,这很好:

我使用你可以在这里看到的类MessageFormat。现在我想把它封装到一个方法中,这个方法就是
getParameterizedMessage(String键,String[]parameters)
,但我不知道怎么做。现在,只显示一两条带有参数的消息需要做大量工作:

UserMessage um = null;   
ResourceBundle messages = ResourceBundle.getBundle("messages");
String str = messages.getString("PF1");
Object[] messageArguments = new String[]{nyreg.getNummer()};
MessageFormat formatter = new MessageFormat("");
formatter.applyPattern(messages.getString("PI14"));
String outputPI14 = formatter.format(messageArguments);
formatter.applyPattern(messages.getString("PI15"));
String outputPI15 = formatter.format(messageArguments)
if(ipeaSisFlag) 
if(checkIfPCTExistInDB && nyreg.isExistInDB()) {            
//um = new ExtendedUserMessage(MessageHandler.getParameterizedMessage("PI15", new String[]{nyreg.getNummer()}) , UserMessage.TYPE_INFORMATION, "Info");
um = new ExtendedUserMessage(outputPI15 , UserMessage.TYPE_INFORMATION, "Info");
……等等。现在,我可以将此逻辑移动到一个静态类MessageHandler.getParameterizedMessage,该类现在不工作,如下所示:

private final static String dictionaryFileName="messages.properties";

public static String getParameterizedMessage(String key, String [] params){
        if (dictionary==null){
            loadDictionary();
        }
        return getParameterizedMessage(dictionary,key,params);
    }

    private static void loadDictionary(){       
        String fileName = dictionaryFileName;   
                try {
            dictionary=new Properties();
            InputStream fileInput = MessageHandler.class.getClassLoader().getResourceAsStream(fileName);
            dictionary.load(fileInput);
            fileInput.close();
        }
        catch(Exception e) {
            System.err.println("Exception reading propertiesfile in init "+e);
            e.printStackTrace();
            dictionary=null;
        }
    }
如何使使用参数化消息与调用带有键和参数的方法一样简单

谢谢你的帮助

更新 该逻辑来自一个继承的方法,该方法位于扩展的抽象类中。该方法如下所示:

    protected static String getParameterizedMessage(Properties dictionary,String key,String []params){
        if (dictionary==null){
            return "ERROR";
        }
        String msg = dictionary.getProperty(key);
        if (msg==null){
            return "?!Meddelande " +key + " saknas!?";
        }
        if (params==null){
            return msg;
        }
        StringBuffer buff = new StringBuffer(msg);
        for (int i=0;i<params.length;i++){
            String placeHolder = "<<"+(i+1)+">>";
            if (buff.indexOf(placeHolder)!=-1){
                replace(buff,placeHolder,params[i]);
            }
            else {
                remove(buff,placeHolder);
            }
        }
        return buff.toString();
    }

我真的不确定你想要实现什么,以下是我过去所做的:

public static final String localize(final Locale locale, final String key, final Object... param) {
    final String name = "message";
    final ResourceBundle rb;

    /* Resource bundles are cached internally,
       never saw a need to implement another caching level
     */
    try {
        rb = ResourceBundle.getBundle(name, locale, Thread.currentThread()
                .getContextClassLoader());
    } catch (MissingResourceException e) {
        throw new RuntimeException("Bundle not found:" + name);
    }

    String keyValue = null;

    try {
        keyValue = rb.getString(key);
    } catch (MissingResourceException e) {
        // LOG.severe("Key not found: " + key);
        keyValue = "???" + key + "???";
    }

    /* Message formating is expensive, try to avoid it */
    if (param != null && param.length > 0) {
        return MessageFormat.format(keyValue, param);
    } else {
        return keyValue;
    }
}

我真的不确定你想要实现什么,以下是我过去所做的:

public static final String localize(final Locale locale, final String key, final Object... param) {
    final String name = "message";
    final ResourceBundle rb;

    /* Resource bundles are cached internally,
       never saw a need to implement another caching level
     */
    try {
        rb = ResourceBundle.getBundle(name, locale, Thread.currentThread()
                .getContextClassLoader());
    } catch (MissingResourceException e) {
        throw new RuntimeException("Bundle not found:" + name);
    }

    String keyValue = null;

    try {
        keyValue = rb.getString(key);
    } catch (MissingResourceException e) {
        // LOG.severe("Key not found: " + key);
        keyValue = "???" + key + "???";
    }

    /* Message formating is expensive, try to avoid it */
    if (param != null && param.length > 0) {
        return MessageFormat.format(keyValue, param);
    } else {
        return keyValue;
    }
}

我们可以看到GetParameterizeMessage(属性字典、字符串键、字符串[]参数)的代码吗?这种方法怎么不起作用?例外?意外的结果?那么,什么是错误的结果?什么是预期的结果?@Pablo我已经用你要求的代码更新了问题。我在基类中找到了它。看起来我需要改变的就是这个方法,因为以前使用
标记而不是粗括号
[0}
用于参数。它现在的工作方式是参数没有出现,即当我使用带花括号{}的参数而不是标记升级自定义JSTL格式时,参数化机制中断了。我们可以查看GetParameterizeMessage(属性字典、字符串键、字符串)的代码吗[]参数)?该方法如何不起作用?异常?意外结果?那么,什么是错误的结果?预期结果是什么?@Pablo我已经用您要求的代码更新了问题。我在基类中找到了它。看起来我需要更改此方法,因为以前使用
标记,而不是参数的粗括号
[0}
。它现在的工作方式是参数没有出现,即当我使用带有大括号{}的参数从自定义JSTL格式升级文件格式时,参数化机制中断了而不是标记>,它看起来可以做到。我将尝试此代码。我不使用区域设置,但带来它不会有任何伤害。它看起来可以做到。我将尝试此代码。我不使用区域设置,但带来它不会有任何伤害。