【问题标题】:Velocity Engine Ignoring @ character?速度引擎忽略@字符?
【发布时间】:2012-09-28 03:15:35
【问题描述】:

我正在使用velocity v1.7 通过JDK v1.7 发送电子邮件

我有以下代码:

VelocityContext context = new VelocityContext();
String name = "myname@yahoo.com";
context.put("userName", name);

在电子邮件模板 (abc.vm) 中,我使用了以下代码:

#if(${userName})        
    Dear ${userName},<br><br>
#end

不知何故,当我收到电子邮件时,它只是说

Dear  

而不是

Dear myname@yahoo.com

如果我将 java 代码更改为不带 @ 字符的正确名称,则电子邮件会正确显示

Dear myName

谁能告诉我为什么@字符被跳过,事实上,为什么它跳过整个名字而不包括在电子邮件中????

谢谢!

【问题讨论】:

  • 试试myname\\@yahoo.com

标签: java velocity


【解决方案1】:

我试图重现,但问题似乎出在电子邮件发送的某个地方。速度本身就可以很好地解决这个问题。

您可以通过将 Velocity 的结果写入输出、日志或文件来缩小此类问题的范围,以便您查看此时的文本内容。调试还会向您显示返回的速度以及在电子邮件发送中出现特殊字符问题的位置。

代码: VelocityContext context = new VelocityContext();

    context.put("name", "Velocity@mycompany.com");

    String name = "myname@yahoo.com";
    context.put("userName", name);

    Template template = null;

    template = Velocity.getTemplate("test.vm");

    StringWriter sw = new StringWriter();

    try {
        template.merge(context, sw);
    } finally {
        sw.close();
    }
    System.out.println(sw);

模板:

Hello $name!

#if(${userName})        
    Dear ${userName},<br><br>
#end

结果输出:

Hello Velocity@mycompany.com!

    Dear myname@yahoo.com,<br><br>

【讨论】:

    猜你喜欢
    • 2021-09-24
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-13
    • 2021-12-08
    相关资源
    最近更新 更多