Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

java - Simple way to use parameterised UI messages in Wicket?

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.

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

msg=Value is {0}

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.

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Take a look at Example 4 in the StringResourceModel javadoc - you can pass a null model and explicit parameters:

add(new Label("message",
         new StringResourceModel(
             "msg", this, null, value)));

msg=Value is {0}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...