zoukankan      html  css  js  c++  java
  • 利用MessageFormat实现短信模板的匹配

    其实没什么技术含量,因为老是想不起来,所以在此文做下记录。

    通常我们的应用系统中都会有很多短信的发送,或者是信息邮件等的推送,而这些信息却有着相同的共性,比如只是用户名换了下。

    像下面这条,除了红色字体外,其余都是相同的。

    尊敬的客户:您的支付宝账户110****11签约网商贷于2018-06-10应还1999.99元,系统将于2018-06-10从该支付宝账户余额、绑定储蓄卡、余额宝、您的网商银行结算账户及余利宝(如有)自动扣款,因余额和储蓄卡还贷款有限额限制,请优先确保余额宝、网商银行结算账户余额、余利宝资金充足。(若已还款请忽略此信息)【网商银行】

    可能不同的同学有不同的处理方法,这里提供下我的解决方案供大家参考,当然如果有更好的方案可以共享下哈~

    方案一:硬编码模式

    可以在代码中硬编码,或者在配置文件中读取。应该有过编码经验的同学都会写

    优点:消息内容直观可见,代码中可直接修改短信内容

    缺点:代码冗余不简洁,硬编码导致修改消息内容不可避免的需要重启服务器。

    方案二:存储在数据库

    将共性的消息模板,存储在服务器中,需要时,直接从数据库中读取解析。

    读取后的解析相信也是千人千法,各有各的方案,各有各的优缺点。

    这里我使用的是java.text包中MessageFormat.format方法,可以方便的匹配解析我们的模板消息。

    MessageFormat方法的介绍

    MessageFormat用来格式化一个消息,通常是一个字符串,比如:

    String str = "I'm not a {0}, age is {1,number,short}, height is {2,number,#.#}";

    而MessageFormat可以格式化这样的消息,然后将格式化后的字符串插入到模式中的适当位置,比如:

    将str中的{0}用"pig"替换,{1,number,short}用数字8替换,{2,number,#.#}用数字1.2替换。

    那么最终用户得到的是一个格式化好的字符串"I'm not a pig, age is 8, height is 1.2"。

    MessageFormat本身与语言环境无关,而与用户提供给MessageFormat的模式和用于已插入参数的子格式模式有关,以生成适用于不同语言环境的消息。

    MessageFormat模式(主要部分): 

    FormatElement:
             { ArgumentIndex }
             { ArgumentIndex , FormatType }
             { ArgumentIndex , FormatType , FormatStyle }

     FormatType
             number

             date

             time

             choice(需要使用ChoiceFormat)

     FormatStyle:
             short
             medium
             long
             full
             integer
             currency
             percent
             SubformatPattern(子模式)

    还以str为例,在这个字符串中:

    1、{0}和{1,number,short}和{2,number,#.#};都属于FormatElement,0,1,2是ArgumentIndex。

    2、{1,number,short}里面的number属于FormatType,short则属于FormatStyle

    3、{1,number,#.#}里面的#.#就属于子格式模式。

    指定FormatTypeFormatStyle是为了生成日期格式的值、不同精度的数字、百分比类型等等。

    实例:

    1、ArgumentIndex必须是非负整数,它的个数不只限于0到9这10个,它可以用0到9的数字组成,因此可以有好多个,如:

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

    最终结果是:ABCDEFGHIJKLMNOPQ

    2、格式化字符串时,两个单引号才表示一个单引号,单个单引号会被省略,如:

    String message = "oh, {0} is 'a' pig";  
      
    Object[] array = new Object[]{"ZhangSan"};  
      
    String value = MessageFormat.format(message, array);  
      
    System.out.println(value);  

     最终结果是:oh, ZhangSan is a pig

    给字母a加上单引号,如:

    String message = "oh, {0} is ''a'' pig";  
      
    Object[] array = new Object[]{"ZhangSan"};  
      
    String value = MessageFormat.format(message, array);  
      
    System.out.println(value);

     最终结果是:oh, ZhangSan is 'a' pig

    3、单引号会使某个字符或串保持原形。

         所以,假如没有特殊要求,一般都是要在正式格式化之前把单引号都去掉,否则会造成不必要的麻烦,如:

    String message = "oh, '{0}' is a pig";  
      
    Object[] array = new Object[]{"ZhangSan"};  
      
    String value = MessageFormat.format(message, array);  
      
    System.out.println(value);  

     最终结果是:oh, {0} is 'a' pig,此处ZhangSan无法显示。

     又如,使用子格式模式,多了一个单引号:

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

     最终结果是:oh, {0,number,#.#}  is 'a' pig。

     如果像下面这样,就可以正确显示。

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

     最终结果是:oh, 3.1 is a pig

    3、无论是有引号字符串还是无引号字符串,左花括号都是不支持的,但支持右花括号显示,如:

    String message = "oh, { is a pig";  
      
    Object[] array = new Object[]{"ZhangSan"};  
      
    String value = MessageFormat.format(message, array);  
      
    System.out.println(value);

     最终结果是:异常java.lang.IllegalArgumentException: Unmatched braces in the pattern

    右花括号可以显示,如:

    String message = "oh, } is a pig";  
      
    Object[] array = new Object[]{"ZhangSan"};  
      
    String value = MessageFormat.format(message, array);  
      
    System.out.println(value);  

     最终结果是:oh, } is a pig

    关于MessageFormat.format方法:

    每调用一次MessageFormat.format方法,都会新创建MessageFormat的一个实例,相当于MessageFormat只使用了一次。MessageFormat类的format方法如下:

    public static String format(String pattern, Object ... arguments)   
    {  
        MessageFormat temp = new MessageFormat(pattern);  
        return temp.format(arguments);  
    }  

    如果要重复使用某个MessageFormat实例,可以用如下方式:

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

     最终结果是:oh, ZhangSan is a pig

     本文实例部分来源自:https://blog.csdn.net/zhiweianran/article/details/8666992

  • 相关阅读:
    Android中连接蓝牙设备时遇到createRfcommSocketToServiceRecord的UUID问题和BluetoothSocket的connect失败
    android4.0蓝牙使能的详细解析 (转载)
    蓝牙介绍
    Bluetooth 4.0之Android 讲解
    jQuery来源学习笔记:扩展的实用功能
    Linux 0.12 内核管理存储器
    java战斗系列-战斗MAVENPW结构
    牟大哥:《App自我促销》连载2 直立人迁移走
    AC自己主动机 总结
    SpringMVC 上下文webApplicationContext
  • 原文地址:https://www.cnblogs.com/laoyeye/p/9164435.html
Copyright © 2011-2022 走看看