35
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}参数没有填充正确的值,但是{0}

有什么想法可能是错的吗?我怀疑这可能是一些语言环境问题。

4

4 回答 4

71

问题解决了!问题似乎是因为邮件 mailconfirm.mail.body 在 {0} 之后和 {1} 之间的某处包含一个撇号。换doesn't了之后does not问题就解决了。我不知道那里不能使用撇号。PS这是一个错误还是我的错误和撇号应该被转义?

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>

我花了大约一个doesn't小时才弄清楚并推动修复。哈哈哈..从现在开始我认为撇号是邪恶的!

于 2011-06-13T15:36:53.733 回答
37

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

但是,默认情况下,不包含任何参数的消息不会被MessageFormat. 因此,不需要对不带参数的消息中的单引号进行转义。

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

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

于 2013-10-04T17:27:20.560 回答
8

我无法说服我的业务团队在需要的地方添加双引号,有时他们也忘记了。所以我只是重写ReloadableResourceBundleMessageSource#loadProperties为:如果值包含"'"& "{0",然后用相同的键替换"'""''"放入 Properties 中。

于 2014-07-12T17:42:03.433 回答
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() );

希望它有用。

于 2018-12-28T10:54:24.877 回答