Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="0607d011a7f9fff3-a38c96ac-417946af-aa43b39d-25075e382ade97e9dc74dd54"><ac:plain-text-body><![CDATA[

[http://jakarta.apache.org/commons/resources/ Home]

[wiki:Resources Wiki]

[wiki:ResourcesUserGuide ContentsGuide]

[wiki:ResourcesUserGuideIntro Getting Started]

[wiki:ResourcesUserGuideMessages Messages]

[wiki:ResourcesUserGuideImplementations Standard]

[wiki:ResourcesUserGuideCreating Creating]

[http://jakarta.apache.org/commons/resources/apidocs/ API]

[http://jakarta.apache.org/commons/resources/xref/ Source]

]]></ac:plain-text-body></ac:structured-macro>

...

2. Using Message and MessageList

2.1 Message

Wiki Markup
\[http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/Message.html Message\] is an encapsulation of an individual message stored in a MessageList object, consisting of a message key (to be used to look up message text in an appropriate Resources implentation) plus up to four placeholder objects that can be used for parametric replacement in the message text.

Message

...

defines two methods:

  • getKey() - return the message key
  • getValues() - return an array of replacement values

Wiki Markup
\[http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/impl/BasicMessage.html BasicMessage\] is a basic implementation of a *Message* and provides convenience constructors for creating a Message with 1, 2, 3 or 4 replacement values.

For example, to create a message with two replacement values (100 and 200 in this case):

No Format

        Message message = new BasicMessage("error.range", "100", "200");

2.2 Message List

...

Wiki Markup
\[http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/MessageList.html MessageList\] a class that encapsulates messages. Messages can be either global or they are specific to a particular bean property. Each individual message is described by a *Message* object. MessageList, as its name implies, has similarities to a _java.util.List_ and provides methods for adding and retrieving messages.

Wiki Markup
\[http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/impl/BasicMessageList.html BasicMessageList\] is a basic implementation of a *MessageList*.

No Format

        MessageList list = new BasicMessageList();

Messages can be stored under a Global Message Key or they can be stored under a specific Property Key

No Format

        list.add(message);            // stored for the Global Message Key
        list.add("OrderNo", message); // stored for property "OrderNo"

Sets of messages can be retrieved from the list either for a property, for the global message key or all messages:

No Format

        Iterator allMsgs     = list.get();
        Iterator orderNoMsgs = list.get("OrderNo");
        Iterator globalMsgs  = list.get(getGlobalMessageKey());

2.3 Messages

Wiki Markup
\[http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/Messages.html Messages\] is a _wrapper_ around any \[http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/Resources.html Resources\] object that performs message string lookups from the Resources instance, and parameter replacement via *java.text.MessageFormat*.  For convenience, the same functionality is also available via static methods that accept a Resources parameter.

First you need to create the Messages object...

No Format

        Resources resources = factory.getResources("Bar", "file:c:/myapp/foo/Bar");
        Messages messages = new Messages(resources);

For example to retrieve the text for a message key "error.maximum" for the US locale with one replacement argument...

No Format

        String text = messages.get(Locale.US, "error.maximum", 1000);

Alternatively you can use the static methods...

No Format

        String text = Messages.get(resources, Locale.US, "error.maximum", 1000);

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="93656c9275097a12-1070e469-44f346dd-b7d686c2-3dc8b09d52b15cfea03ccb99"><ac:plain-text-body><![CDATA[

[http://jakarta.apache.org/commons/resources/ Home]

[wiki:Resources Wiki]

[wiki:ResourcesUserGuide ContentsGuide]

[wiki:ResourcesUserGuideIntro Getting Started]

[wiki:ResourcesUserGuideMessages Messages]

[wiki:ResourcesUserGuideImplementations Standard]

[wiki:ResourcesUserGuideCreating Creating]

[http://jakarta.apache.org/commons/resources/apidocs/ API]

[http://jakarta.apache.org/commons/resources/xref/ Source]

]]></ac:plain-text-body></ac:structured-macro>