Simple way to use parameterised UI messages in Wicket?

后端 未结 6 637
借酒劲吻你
借酒劲吻你 2021-01-02 02:43

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

6条回答
  •  离开以前
    2021-01-02 03:34

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

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

    Why I like it:

    • 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.

    Notes:

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

    and pass it as MessageFormat argument.

    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.

提交回复
热议问题