mailconfirm.mail.body=<html><body><h3 style="margin: 0 0 1em;">Hi, {0}!</h3>\
    To confirm your email address click on the confirmation link given bellow. If clicking on the link doesn't work, copy and paste the link in a new browser tab. <br /><br />\
    <a href="http://www.domain.com/confirm_email.html?action=activate&hash={1}">http://www.domain.com/confirm_email.html?action=activate&hash={1}</a><br /><br />\
    Kind regards,<br />\
    Your Something
    </body></html>

这上面是特别信息用于代码波纹管。

String country = "AU";
Object[] args = new Object[] { account.getLogin(), confirm.getHash() };

helper.setText(appContext.getMessage("mailconfirm.mail.body", args,
                new Locale(country)), true);

我调试这两个参数和他们两个都有正确的价值观。在调试 appContext.getMessage 行,我看到的 {1} param是不是充满了正确的价值,但是 {0} 是。

任何想法什么可能是错的?我怀疑这可能是一些区域问题。

有帮助吗?

解决方案

问题解决了! 看来问题出在此是因为邮件mailconfirm.mail.body在{0}之后到{1}之间包含撇号。用doesn't替换does not后,它解决了该问题。我不知道在其中不能使用撇号。 附言是一个错误还是仅仅是我的错误而撇号应该被转义? 通用标签

一个doesn't花了我大约一个小时的时间来弄清楚它并进行修复。哈哈哈..从现在开始,我认为撇号是邪恶的!

其他提示

Spring的ResourceBundleMessageSource(我认为您正在使用)使用MessageFormat替换消息中的占位符({0})。MessageFormat要求使用两个单引号(')对单引号('')进行转义(请参阅: MessageFormat Javadoc )。

但是,默认情况下,MessageFormat不会解析不包含任何参数的消息。因此,消息中没有参数的单引号不需要转义。

ResourceBundleMessageSource提供了一个名为alwaysUseMessageFormat的标志,如果将MessageFormat应用于所有消息,则可以使用该标志。因此,单引号总是需要被两个单引号转义。

有关更多详细信息,请参见此博客文章

我无法说服我的业务团队在所需位置添加双引号,有时他们也忘记了。 所以我只是将ReloadableResourceBundleMessageSource#loadProperties重写为: 如果值包含"'""{0",则将"'"替换为"''",然后使用相同的键将其放入“属性”中。

一种替代方法是使用 String.format, ,改变 {X} 对于 %s.

mailconfirm.mail.body=<html><body><h3 style="margin: 0 0 1em;">Hi, %s!</h3>\
    To confirm your email address click on the confirmation link given bellow. If clicking on the link doesn't work, copy and paste the link in a new browser tab. <br /><br />\
    <a href="http://www.domain/confirm_email.html?action=activate&hash=%s">http://www.domain/confirm_email.html?action=activate&hash=%s</a><br /><br />\
    Kind regards,<br />\
    Your Something
    </body></html>


String whatEver = messageSource.getMessage("mailconfirm.mail.body", null, locale);

whatEver = String.format(whatEver,account.getLogin(), confirm.getHash() );

希望这是有用的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top