Velocity 模板似乎无法使用 UTF-8
我一直在尝试使用包含以下内容的速度模板:
Sübjäct $item
除了两个 Unicode 字符的翻译之外,一切正常。命令行上打印的结果字符串如下所示:
Sübjäct foo
我在速度网站和网络上搜索了这个问题,并提出了不同的字体编码选项,并将其添加到我的代码中。但这些没有帮助。这是实际的代码:
velocity.setProperty("file.resource.loader.path", absPath);
velocity.setProperty("input.encoding", "UTF-8");
velocity.setProperty("output.encoding", "UTF-8");
Template t = velocity.getTemplate("subject.vm");
t.setEncoding("UTF-8");
StringWriter sw = new StringWriter();
t.merge(null, sw);
System.out.println(sw.getBuffer());
我如何解决这个问题?
I have been trying to use a velocity Template with the following content:
Sübjäct $item
Everything works fine except the translation of the two Unicode characters. The result string printed on the command line looks like:
Sübjäct foo
I searched the velocity website and the web for this issue, and came up with different font encoding options, which I added to my code. But these don't help. This is the actual code:
velocity.setProperty("file.resource.loader.path", absPath);
velocity.setProperty("input.encoding", "UTF-8");
velocity.setProperty("output.encoding", "UTF-8");
Template t = velocity.getTemplate("subject.vm");
t.setEncoding("UTF-8");
StringWriter sw = new StringWriter();
t.merge(null, sw);
System.out.println(sw.getBuffer());
How an I fix this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您尝试过使用这种语法吗?
看起来它应该做正确的事情。
Have you tried using this syntax?
That looks like it should do the right thing.
如果您将 VelocityEngine 与 JavaMailSenderImpl 类一起使用,请不要忘记设置 defaultEncoding 属性。另外,如上所述,尝试为VelocityEngine类配置input.encoding和output.encoding属性。我在下面留下一个例子。
If you're using VelocityEngine along with JavaMailSenderImpl class, don't forget to set the defaultEncoding property. Also, as mentioned above, try configuring input.encoding and output.encoding properties for the VelocityEngine class. I leave an example below.
我的解决方案:将“-Dfile.encoding=UTF-8”添加到jvm选项(不包括引号)。
我尝试了上述可能的解决方案,但没有一个对我有用。
经过几天的扭曲搜索和探索,我猜我的问题发生在velocitie渲染html文件上,因为我发现一些错误显示的文本实际上是GB2312编码的,我意识到当页面打开时vm文件的编码不正确渲染(我猜)。
My solution: add "-Dfile.encoding=UTF-8" to jvm option(quotes not included).
I try the above possible solution, none of then works for me.
After days of twisted search and explore, I guess my problem happen on velocitie's rendering the html file, because I found some wrongly displayed text is actually in GB2312 encoding, I realised that the encoding of the vm file is not correct when the page is been rendering(I guess).
如果您手动创建
JavaMailSenderImpl
实例,则可以通过以下方式设置编码属性:如果您使用
MimeMessageHelper
,请按如下方式设置编码:最后,对于 Velocity引擎,设置编码时有两种方式获取/应用模板:
If you're creating a
JavaMailSenderImpl
instance manually, you can set the encoding property this way:If you're using
MimeMessageHelper
, set the encoding as follows:Finally, for Velocity Engine, there are two ways to get/apply the template while setting the encoding: