美文网首页
字符串模版替换

字符串模版替换

作者: Hey_Shaw | 来源:发表于2018-03-23 11:32 被阅读11次

背景:
工程框架里的短信功能,有个模版配置,模版配置里会根据客户信息设置相应的参数,源框架别人开发用的是正则表达式加String.replace(),实在太不灵活,太low。

能力有限,只有下面这个笨拙的方法(有大牛经过,望止步指点一二)

工具

下面直接贴代码,大家觉得有用,直接搬砖,有更好的改造,希望可以共享,支持开源精神。

FreemarkerTest.java

import java.io.IOException;
import java.io.StringWriter;

import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.utility.NullArgumentException;

public class FreemarkerTest {

   private static String REPLACEERROR = "替换失败,请检查参数是否有误" ;
   
   private static Configuration cfg;
   private static StringTemplateLoader stl;
   
   static {
       cfg = new Configuration(Configuration.VERSION_2_3_0);
       stl = new StringTemplateLoader();
   }


   /**
    * 单个模版,单个对象属性替换
    * 
    * @throws NullArgumentException
    * 
    * @param tempname 模版名称,唯一标示
    * @param stringtemp 模版字符串
    * @param object 模版属性的类对象
    * @return 替换后生成的字符串
    */
   public static String stringTemplateReplaceForObject(String tempname, String stringtemp, Object object) {
       
       if (stringtemp == null || stringtemp.length() <= 0) {
           throw new NullArgumentException("替换模版为空,不允许!");
       }

       if (object == null) {
           throw new NullArgumentException("替换对象不要传空!");
       }
       try {
           stl.putTemplate(tempname, stringtemp);
           cfg.setTemplateLoader(stl);
           Template template = cfg.getTemplate(tempname);
           StringWriter writer = new StringWriter() ;
           template.process(object, writer);
           return writer.toString();
       } catch (TemplateException e) {
            e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }
       return REPLACEERROR;
   }
   
   /**
    * 模版二次替换,需与stringTemplateReplaceForObject(xxx,xxx,xxx)方法结合使用,不然会出现不存在指定的模版异常
    * 
    * @see #stringTemplateReplaceForObject(String, String, Object)
    * 
    * @param tempname 模版名称
    * @param object  替换的对象
    * @return 替换后生成的字符串
    */
   public static String stringTempReplaceToTempNameForObject(String tempname, Object object) {
       try {
           Template template = cfg.getTemplate(tempname);
           StringWriter writer = new StringWriter() ; // StringWriter会缓存,不能设置为全局
           template.process(object, writer);
           return writer.toString();
       } catch (IOException e) {
           e.printStackTrace();
       } catch (TemplateException e) {
           e.printStackTrace();
       }
       return REPLACEERROR;
   }
}

测试

public static void main(String[] args) throws Exception{
        Person person = new Person() ;
        person.setName("替换成张三");
        String s = FreemarkerTest.stringTemplateReplaceForObject("person", "hello:${name}", person);
        System.out.println(s);
        Student student = new Student();
        student.setAge(25);
        Map<String,Object> map = new HashMap<>() ;
        map.put("person", person);
        map.put("student", student);
        s = FreemarkerTest.stringTemplateReplaceForObject("student", "人的名字是:${person.name},学生年龄:${student.age}", map);
        System.out.println(s);
        person.setName("把名字换成李四");
        s= FreemarkerTest.stringTempReplaceToTempNameForObject("person",person);
        System.out.println(s);
    }

相关文章

  • 字符串模版替换

    背景:工程框架里的短信功能,有个模版配置,模版配置里会根据客户信息设置相应的参数,源框架别人开发用的是正则表达式加...

  • ES6模版字符串

    认识一下模版字符串 模版字符串的注意事项 输出多行字符串 输出`和\等特殊符号 模版字符串的注入 ${} 模版字符...

  • Android中string.xml应用

    配置字符串替换 单个字符串替换 使用Context方法,动态替换 多个个字符串替换 配置字符串中的空格

  • js替换字符串

    1、替换第一个字符串: 2、替换所有字符串 3、替换所有key的字符串

  • String

    字符串的遍历器接口 ES6为字符串添加了遍历器接口,使得字符串可以被for...of循环遍历 模版字符串 模版字符...

  • 实现模板引擎

    字符串替换 Template(模板) 目录 字符串拼接 string format(字符串格式化) 模板替换 自制...

  • linux-对文件某个字符串替换,批量替换

    全部替换:sed -i 's/查找的字符串/替换的字符串/g' 文件 替换每行第一次出现的字符串:sed -i '...

  • Vim常用配置

    替换 语法为 :[addr]s/源字符串/目的字符串/[option]全局替换命令为::%s/源字符串/目的字符串...

  • sed

    1、替换1.1 sed 's/原字符串/替换字符串/'单引号里面,s表示替换,三根斜线中间是替换的样式,特殊字符需...

  • sed中转义字符的替换

    sed中转义字符的替换 背景 在使用sed的时候,经常我们会用来替换字符串,如果被替换的字符串中包含转移字符串怎么...

网友评论

      本文标题:字符串模版替换

      本文链接:https://www.haomeiwen.com/subject/pmgtcftx.html