Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FreeMarker encoding confusion

When I read an UTF-8 encoded template with FreeMarker, special chars are rendered correctly in the browser, although freeMarkerConfig.getDefaultEncoding() returns "Cp1252". If I set freeMarkerConfig.setDefaultEncoding("UTF-8"), I see only question marks in the browser, although "UTF-8" is the actual encoding of the template file. In every case the http header "Content-Type: text/html; charset=UTF-8" is sent.

Any idea what is wrong?

like image 786
deamon Avatar asked Aug 08 '10 11:08

deamon


People also ask

What is FreeMarker Template Error?

FreeMarker template error appears in logs when attempting to add a Web Content Article to a page or search for it in the Search portlet. The issue only occurs when the Summary field is empty and when using the following line in the template.

Does FreeMarker have content?

The has_content FunctionFreeMarker has built-in functions to detect for variables. The most common method of detecting for empty or null values uses the has_content function and the trim function. The has_content function is often used with another built-in FreeMarker function to detect for null values.

How do you escape FreeMarker?

Turns on auto-escaping in the nested section. Auto-escaping is usually enabled by default if the current output format has auto-escaping by default, so you rarely need this. Note that to escape just a single ${expression} where auto-escaping is disabled you should use ${expression? esc} instead.

Is FreeMarker a programming language?

Although FreeMarker has some programming capabilities, it is not a full-blown programming language like PHP. Instead, Java programs prepare the data to be displayed (like issue SQL queries), and FreeMarker just generates textual pages that display the prepared data using templates.


2 Answers

Set the content type property into the FreeMarkerViewResolver.

Spring 4.2 example

@Bean
public FreeMarkerViewResolver freemarkerViewResolver() {
    FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
    resolver.setContentType("text/html; charset=utf-8");
    resolver.setCache(true);
    resolver.setPrefix("");
    resolver.setSuffix(".ftl.html");
    resolver.setRequestContextAttribute("rc");
    return resolver;
}
like image 83
Daniel De León Avatar answered Oct 19 '22 18:10

Daniel De León


In the case you are using spring framework and a MimeMessage to send the email try setting content via a MimeMessagePreparator as follows (I skip the mimemessagepreparator method getMessagePreparator as the important thing is how to set content):

// Create the message helper from the received mimemessage on the preparator
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
// ... include the from, to, subject, freemarker html generation here... text is the String variable with generated html
// Set the content as follows instead of helper.setText(text, true);
helper.getMimeMessage().setContent(text, "text/html;charset=utf-8");

This worked for me and browsers are displaying chars correctly when sending emails.

The implied classes are:

import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import javax.mail.internet.MimeMessage;

Also be sure that your workspace has a default encoding of UTF-8 for the template files by right clicking on them looking properties if you are using eclipse IDE.

Hope this helps.

like image 28
Francisco Valle Avatar answered Oct 19 '22 19:10

Francisco Valle