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?
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).
Have you tried using this syntax?
That looks like it should do the right thing.
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:
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.