Alex Rivera | Logout

FreeMarker encoding confusion

Asked 2010-08-08T11:12:21.497
10

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?

Edit
Report

1 Answer

0

The output encoding is those of your java machinery. If you create an output file with UTF-FOO, and pass this output file to freemarker generation, the output encoding will be UTF-FOO.

See Charset issues.

With ex. code :

  Template templévénmts;
  BufferedWriter writ;
  OutputStreamWriter encodé;

  encodé = new OutputStreamWriter(
   new FileOutputStream(new File(f_dirDestination, résultat)), "UTF-8");
  writ = new BufferedWriter(
   encodé);
  templévénmts = f_freemarker.getTemplate(modèle);
  templévénmts.process(f_rootDatas, writ);
  writ.close();

You can also use FileWriterWithEncoding in commons io.

answered 2010-08-08T13:47:06.147

Your Answer