[英]How to use velocity for SMS templates
I am working on a Java service that needs to send SMS through Amazon SNS.我正在开发一项需要通过 Amazon SNS 发送 SMS 的 Java 服务。
I am using Velocity templates to generate personalized emails, and thought about using it for SMS as well.我正在使用 Velocity 模板来生成个性化的电子邮件,并考虑将其用于 SMS。
But I don't think it is the right approach because the AWS SDK method for sending an SMS takes the message as a string.但我认为这不是正确的方法,因为用于发送 SMS 的 AWS SDK 方法将消息作为字符串。 This would force me to generate a file and then read it to get the contents as a string.这将迫使我生成一个文件,然后读取它以获取字符串形式的内容。
The only alternative I can think of is storing the template as TINYTEXT (SMS size limit is 140 bytes) in the database, and use String.replaceAll()
instead of velocity.我能想到的唯一替代方法是在数据库中将模板存储为 TINYTEXT(SMS 大小限制为 140 字节),并使用String.replaceAll()
而不是速度。
But I wanted to know if there is a better way to do it or if using velocity would hurt performance that much.但我想知道是否有更好的方法来做到这一点,或者使用速度是否会严重影响性能。
You can use velocity without generating a file until VelocityEngine.evaluate您可以在不生成文件的情况下使用速度,直到VelocityEngine.evaluate
renders the input string using the context into the output writer.使用上下文将输入字符串呈现到输出编写器中。 To be used when a template is dynamically constructed, or want to use Velocity as a token replacer.在动态构造模板时使用,或希望使用 Velocity 作为标记替换器。
VelocityContext context = new VelocityContext(); context.put("param", paramMap); context.put("placeList", placeList); StringWriter writer = new StringWriter(); ve.evaluate(context, writer, "", template); return writer.toString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.