Actually, there is no technical content, because I can't remember it, so I will record it in this article.
Usually, there are many SMS messages in our application system, or push messages, etc., but these messages have the same commonality, such as just changing the username.
Like the one below, except for the red font, the rest are the same.
Dear customers: Your Alipay account 110****11 contracted online merchant loan should be repaid 1999.99 yuan on 2018-06-10. The system will automatically deduct the balance of the Alipay account, bound savings card, Yu'ebao, your Announcement Bank settlement account and Yu'ebao (if any) on 2018-06-10. Due to the limited limit on balance and savings card loan repayment, please give priority to ensuring that the balance of Yu'ebao, Announcement Bank settlement account, and Yu'ebao are sufficient. (If you have already paid back, please ignore this information) [Anti-Commerce Bank]
Different students may have different solutions. Here are my solutions for your reference. Of course, if there is a better solution, you can share it~
Solution 1: Hard-coded mode
Can be hardcoded in the code, or read in the configuration file. Students who have coding experience should write
Advantages: The message content is intuitive and the text message content can be directly modified in the code
Disadvantages: The code is redundant and not concise, and hard-coded causes the server to be restarted due to the inevitable need to modify the message content.
Solution 2: Store in the database
Store common message templates in the server and read and parse directly from the database when needed.
The analysis after reading is believed to be a thousand people and methods, each with its own solutions, and each with its own advantages and disadvantages.
Here I am using the MessageFormat.format method in java.text包, which can easily match and parse our template messages.
Introduction to MessageFormat method
MessageFormat is used to format a message, usually a string, such as:
String str = "I'm not a {0}, age is {1,number,short}, height is {2,number,#.#}";MessageFormat can format such messages and then insert the formatted string into the appropriate position in the pattern, such as:
Replace {0} in str with "pig", {1,number,short} with the number 8, and {2,number,#.#} with the number 1.2.
Then what the end user gets is a formatted string "I'm not a pig, age is 8, height is 1.2".
MessageFormat itself is not related to the locale, but to the mode provided by the user to MessageFormat and the subformat pattern for inserted parameters to generate messages suitable for different locales.
MessageFormat mode (main part):
FormatElement:
{ ArgumentIndex }
{ ArgumentIndex, FormatType }
{ ArgumentIndex, FormatType, FormatStyle}
FormatType:
number
date
time
Choice (requires ChoiceFormat)
FormatStyle:
Short
medium
long
full
integer
currency
percent
SubformatPattern
Take str as an example, in this string:
1. {0} and {1,number,short} and {2,number,#.#}; both belong to FormatElement, 0,1,2 are ArgumentIndex.
2. The number in {1,number,short} belongs to FormatType, while short belongs to FormatStyle.
3. #.# in {1,number,#.#} belongs to the subformat mode.
Specifying FormatType and FormatStyle is to generate date format values, numbers of different precisions, percentage types, etc.
Example:
1. ArgumentIndex must be a non-negative integer. Its number is not limited to 10 of 0 to 9. It can be composed of numbers from 0 to 9, so there can be many, such as:
String pig = "{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}{16}";Object[] array = new Object[]{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q"}; String value = MessageFormat.format(message, array); System.out.println(value); The final result is: ABCDEFGHIJKLMNOPQ
2. When formatting a string, only two single quotes represent a single quote, and a single single quote will be omitted, such as:
String message = "oh, {0} is 'a' pig"; Object[] array = new Object[]{"ZhangSan"}; String value = MessageFormat.format(message, array); System.out.println(value); The end result is: oh, ZhangSan is a pig
Add single quotes to the letter a, such as:
String message = "oh, {0} is ''a'' pig"; Object[] array = new Object[]{"ZhangSan"}; String value = MessageFormat.format(message, array); System.out.println(value); The end result is: oh, ZhangSan is 'a' pig
3. Single quotes will keep a character or string authentic.
Therefore, if there are no special requirements, the single quotes must be removed before formal formatting, otherwise unnecessary trouble will be caused, such as:
String message = "oh, '{0}' is a pig"; Object[] array = new Object[]{"ZhangSan"}; String value = MessageFormat.format(message, array); System.out.println(value);The final result is: oh, {0} is 'a' pig, ZhangSan cannot be displayed here.
For example, using the subformat pattern, there is an additional single quote:
String message = "oh, '{0,number,#.#} is a pig"; Object[] array = new Object[]{new Double(3.1415)}; String value = MessageFormat.format(message, array); System.out.println(value); The end result is: oh, {0,number,#.#} is 'a' pig。
If it is like this, it can be displayed correctly.
String message = "oh, {0,number,#.#} is a pig"; Object[] array = new Object[]{new Double(3.1415)}; String value = MessageFormat.format(message, array); System.out.println(value); The end result is: oh, 3.1 is a pig
3. Whether it is a quoted string or a non-quoted string, the left curly brace is not supported, but the right curly brace is supported, such as:
String message = "oh, { is a pig"; Object[] array = new Object[]{"ZhangSan"}; String value = MessageFormat.format(message, array); System.out.println(value); The final result is:异常java.lang.IllegalArgumentException: Unmatched braces in the pattern
The right brace can be displayed, such as:
String message = "oh, } is a pig"; Object[] array = new Object[]{"ZhangSan"}; String value = MessageFormat.format(message, array); System.out.println(value); The end result is: oh, } is a pig
About MessageFormat.format method:
Every time the MessageFormat.format method is called, an instance of MessageFormat will be created, which is equivalent to that MessageFormat is only used once. The format method of the MessageFormat class is as follows:
public static String format(String pattern, Object ... arguments) { MessageFormat temp = new MessageFormat(pattern); return temp.format(arguments); }If you want to reuse a MessageFormat instance, you can use the following method:
String message = "oh, {0} is a pig"; MessageFormat messageFormat = new MessageFormat(message); Object[] array = new Object[]{"ZhangSan"}; String value = messageFormat.format(array); System.out.println(value); The end result is: oh, ZhangSan is a pig
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.