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

查看:22
本文介绍了在 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 来替换值;如上例中的 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,别无其他
  • 您可以根据需要替换任意数量的值
  • 使用标签、info()、验证等.
  • 它并不完全是 wickety,但它与 wicket 一致,因此您可以使用 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 参数传递.

and pass it as MessageFormat argument.

我不知道为什么 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天全站免登陆