Re: Freemarker Encoding issue

"Newman, John W" <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <D688C4D318A63D4290AA94A33E06F7D20285CBF13A@msxmbxnsprd14.acct.upmchs.net>
If you are using FreemarkerServlet (sounds like it), you probably need to set the output encoding param in your web.xml

      <servlet>
            <servlet-name>Freemarker</servlet-name>
            <servlet-class>
                  freemarker.ext.servlet.FreemarkerServlet</servlet-class>
            <init-param>
                  <param-name>TemplatePath</param-name>
                  <param-value>/</param-value>
            </init-param>
            <init-param>
                  <param-name>default_encoding</param-name>
                  <param-value>UTF-8</param-value>
            </init-param>
            <init-param>
                  <param-name>output_encoding</param-name>
                  <param-value>UTF-8</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
      </servlet>

Of course, UTF-8 would be whatever encoding you are actually targeting.  I'm not entirely sure that this matters, but make sure that the physical .ftl file itself is saved with the correct encoding and not just ISO-8859-1.  And you might also need the meta tag at the top of the page:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>

We have Chinese characters working from database to page end to end, so it is definitely possible.  It was a challenge to get right - difficult to pin point at what point in the chain they were getting lost.


From: Zhiqiang Huang [mailto:[email protected]]
Sent: Monday, March 08, 2010 10:55 PM
To: Daniel Dekany
Cc: [email protected]
Subject: Re: [FreeMarker-user] Freemarker Encoding issue

I tried as you say, ${"\x6464"} is still output as a '?'. And I have debug my servlet to make sure that the string in java is correct, i.e., do not contain any '?'.
So I want to know how should I set the configuration for FreeMarker and java.io.Writer?
Thank you.

Best regards,
hzqtc
On Tue, Mar 9, 2010 at 6:38 AM, Daniel Dekany <[email protected]<mailto:[email protected]>> wrote:
Monday, March 8, 2010, 5:30:23 PM, Zhiqiang Huang wrote:

> Hi, there
>
> I'm new to freemarker and I'm using freemarker with servlet. I'm coming across a encoding issue.
> If I put chinese in .ftl, it can be output correctly. But when I
> put chinese in data-model (a hashmap), it can't. The Chinese characters are output as '?'.
> I have tried all the setxxxEncoding methods to set the encoding to "utf-8" and failed.
> Could anyone help me? Thank you very much!
Put a ${"\x6464"} into your template. If in the browser it shows a "?"
instead of a chinese character, then something is still wrong with the
FreeMarker configuration and/or with the java.io.Writer where
FreeMarker prints its output. If ${"\x6464"} shows a chinese
character, and also the chiense text entered directly into the
template shows correctly, then most probably it's the connection to
the data-base server that has an encoding issue. (You can double-check
that if in Java (not in FreeMarker) you log the character codes of the
string... if it contains 63-s, which is the code of '?', the it
already contains the '?'-s when you receive it from the database.)

> Best wishes,
> hzqtc
--
Best regards,
 Daniel Dekany

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev

_______________________________________________
FreeMarker-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.