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?
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.
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.
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.
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.
Set the content type property into the FreeMarkerViewResolver.
@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;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With