Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Velocity模板似乎无法使用UTF-8_Java_Utf 8_Velocity - Fatal编程技术网

Java Velocity模板似乎无法使用UTF-8

Java Velocity模板似乎无法使用UTF-8,java,utf-8,velocity,Java,Utf 8,Velocity,我一直在尝试使用包含以下内容的velocity模板: Sübjäct$item 除了两个Unicode字符的翻译外,一切正常。打印在命令行上的结果字符串如下所示: SübjÕct foo 我在velocity网站和web上搜索了这个问题,找到了不同的字体编码选项,并将其添加到我的代码中。但这些都没用。这是实际代码: velocity.setProperty("file.resource.loader.path", absPath); velocity.setProperty("input.e

我一直在尝试使用包含以下内容的velocity模板:

Sübjäct$item

除了两个Unicode字符的翻译外,一切正常。打印在命令行上的结果字符串如下所示:

SübjÕct foo

我在velocity网站和web上搜索了这个问题,找到了不同的字体编码选项,并将其添加到我的代码中。但这些都没用。这是实际代码:

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());

如何解决此问题?

您是否尝试过使用此语法

Template template = Velocity.getTemplate("subject.vm", "UTF-8");

看起来它应该做正确的事情。

如果您将VelocityEngine与JavaMailSenderImpl类一起使用,请不要忘记设置defaultEncoding属性。另外,如上所述,尝试为VelocityEngine类配置input.encoding和output.encoding属性。我在下面留下一个例子

配置文件


UTF-8
UTF-8
UTF-8
文件
org.apache.velocity.runtime.resource.loader.FileResourceLoader
${relative.path}/电子邮件模板
假的
我的解决方案:在jvm选项中添加“-Dfile.encoding=UTF-8”(不包括引号)

我尝试了上述可能的解决方案,但没有一个对我有效


经过几天的扭曲搜索和探索,我猜我的问题发生在velocitie呈现html文件上,因为我发现一些错误显示的文本实际上是GB2312编码,我意识到当页面呈现时vm文件的编码是不正确的(我猜).

如果要手动创建
JavaMailSenderImpl
实例,可以通过以下方式设置encoding属性:

JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
Properties mailProperties = new Properties();
mailProperties.put("defaultEncoding", "UTF-8");
mailSender.setJavaMailProperties(mailProperties);
如果使用的是
mimessagehelper
,请按如下方式设置编码:

MimeMessageHelper helper = new MimeMessageHelper(mailSender.createMimeMessage(), true, "UTF-8");
最后,对于Velocity Engine,有两种方法可以在设置编码时获取/应用模板:

Template template = Velocity.getTemplate("template", "UTF-8");
// or
velocityEngine.mergeTemplate("template", "UTF-8", velocityContext, stringWriter);

嗨,非常感谢。关于另一条评论,velocity实际上是VelocityEngine的一个实例。但代码段无论如何都是一个测试用例,专有名称只存在于生产代码中;)@史东:嗯,OP似乎认为它对他们有效,所以听起来你可能在其他地方有项目。我建议你问一个新问题,展示你的尝试和错误。对不起,我终于找到了根本原因。你的解决方案是有效的。在我的程序上不起作用的原因是我使用了ve.setProperty(“eventhandler.referenceinsertion.class”、“org.apache.velocity.app.event.implement.EscapeXmlReference”);EscapeXmlReference破坏了编码。@goblingift:如果单参数重载使用平台默认编码(许多Java API中都是这种情况),那么这并不特别。(我不知道情况是否如此,但这似乎是一个合理的假设…)这一切都非常有效。如果在velocity模板引擎上出现问题,应该是这种情况吗?该参数应该强制进行所需的编码。虽然这会起作用,但它会在整个JVM中改变很多东西的默认值,这可能不是您想要的。当心在有更多目标选项可用时更改默认值。
Template template = Velocity.getTemplate("template", "UTF-8");
// or
velocityEngine.mergeTemplate("template", "UTF-8", velocityContext, stringWriter);