在Wicket中使用参数化UI消息的简单方法? [英] Simple way to use parameterised UI messages in Wicket?

查看:145
本文介绍了在Wicket中使用参数化UI消息的简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Wicket有一个灵活的国际化系统,支持许多UI消息的参数化方法。有一些例子,例如在 StringResourceModel javadocs中,例如:

Wicket has a flexible internationalisation system that supports parameterising UI messages in many ways. There are examples e.g. in StringResourceModel javadocs, such as this:

WeatherStation ws = new WeatherStation();
add(new Label("weatherMessage", new StringResourceModel(
    "weather.${currentStatus}", this, new Model<String>(ws)));

但我想要一些非常简单的东西,而且找不到一个很好的例子。

But I want something really simple, and couldn't find a good example of that.

在.properties文件中考虑这种UI消息:

Consider this kind of UI message in a .properties file:

msg=Value is {0}

具体来说,我不想创建一个模型对象(带有getter for the要替换的值;如上例中的WeatherStation)仅用于此目的。如果我已经在局部变量中有值,那就太过分了,否则就不需要这样的对象了。

Specifically, I wouldn't want to create a model object (with getters for the values to be replaced; like WeatherStation in the above example) only for this purpose. That's just overkill if I already have the values in local variables, and there is otherwise no need for such object.

这是用正确的值替换{0}的一种愚蠢的蛮力方式:

Here's a stupid "brute force" way to replace the {0} with the right value:

String value = ... // contains the dynamic value to use
add(new Label("message", getString("msg").replaceAll("\\{0\\}", value)));

是否有更干净,更多Wicket-y方式来做到这一点(这不比上面那么长)

Is there a clean, more Wicket-y way to do this (that isn't awfully much longer than the above)?

推荐答案

我认为最一致的 WICKETY 方式可以通过改善 Jonik的回答 MessageFormat

I think the most consistent WICKETY way could be accomplished by improving Jonik's answer with MessageFormat:

.properties:

.properties:

msg=Saving record {0} with value {1}

.java:

add(new Label("label", MessageFormat.format(getString("msg"),obj1,obj2)));
//or
info(MessageFormat.format(getString("msg"),obj1,obj2));

为什么我喜欢它:


  • 干净,简单的解决方案

  • 使用普通Java而不使用其他

  • 您可以根据需要替换任意数量的值

  • 使用标签,信息(),验证等。

  • 这不是完全摇摇晃晃,但它与wicket一致,因此您可以使用<$重复使用这些属性c $ c> StringResourceModel 。

  • Clean, simple solution
  • Uses plain Java and nothing else
  • You can replace as many values as you want
  • Work with labels, info(), validation, etc.
  • It's not completely wickety but it is consistent with wicket so you may reuse these properties with StringResourceModel.

注意:

如果你想使用模型,你只需要创建一个简单的模型,覆盖模型的 toString 函数,如下所示:

if you want to use Models you simply need to create a simple model that override toString function of the model like this:

abstract class MyModel extends AbstractReadOnlyModel{
    @Override
    public String toString()
    {
        if(getObject()==null)return "";
        return getObject().toString();
    }
}

并将其传递为 MessageFormat 参数。

我不知道为什么Wicket在反馈中不支持 Model 信息。但如果支持它没有理由使用这些解决方案,你可以在任何地方使用 StringResourceModel

I don't know why Wicket does not support Model in feedback message. but if it was supported there was no reason to use these solutions and you could use StringResourceModel everywhere.

这篇关于在Wicket中使用参数化UI消息的简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆