Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Velocity templates seem to fail with UTF-8

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?

like image 205
steve Avatar asked Mar 01 '11 07:03

steve


People also ask

Is Velocity template deprecated?

Velocity templates were deprecated in Liferay Portal 7.0 and are now removed in favor of FreeMarker templates in Liferay DXP 7.2.

What is Vlocity template?

¶ Velocity is a Java-based template engine. It permits anyone to use a simple yet powerful template language to reference objects defined in Java code.


3 Answers

Have you tried using this syntax?

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

That looks like it should do the right thing.

like image 161
Jon Skeet Avatar answered Sep 21 '22 19:09

Jon Skeet


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.

Configuration file

<bean id="smtpSession" class="org.springframework.jndi.JndiObjectFactoryBean">         <property name="jndiName" value="java:jboss/example/jndiName"/>     </bean> <bean id="javaMailSenderImpl" class="org.springframework.mail.javamail.JavaMailSenderImpl">         <property name="session" ref="smtpSession"/>         <property name="defaultEncoding" value="UTF-8"/> </bean>  <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">         <property name="velocityProperties">             <props>                 <prop key="input.encoding">UTF-8</prop>                 <prop key="output.encoding">UTF-8</prop>                 <prop key="response.encoding">UTF-8</prop>                 <prop key="resource.loader">file</prop>                 <prop key="file.resource.loader.class">org.apache.velocity.runtime.resource.loader.FileResourceLoader                 </prop>                 <prop key="file.resource.loader.path">${relative.path}/email-templates</prop>                 <prop key="file.resource.loader.cache">false</prop>                                 </props>         </property>     </bean> 
like image 27
darkconeja Avatar answered Sep 18 '22 19:09

darkconeja


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).

like image 39
user5465377 Avatar answered Sep 21 '22 19:09

user5465377