1

I have a JSP Page which uses the following directive:

<%@page contentType="text/html; charset=iso-8859-1" pageEncoding="iso-8859-1"%>

Hence, forms within the page are sent using ISO-8859-1 encoding and the beans receive (in the setXXX() methods) Strings encoded according to this format.

I want to "translate" the JSP into JSF 2, which uses UTF-8 as default. How can I force JSF to use ISO-8859-1 and hence simulate the page directive?

I use Mojarra + richfaces on jboss 6.

Thank you!

Federico
  • 561
  • 2
  • 11
  • 32

2 Answers2

5

Set the encoding attribute of the <f:view>.

<f:view encoding="ISO-8859-1">

Sticking to a non-Unicode encoding is however not recommended anymore these days. Your webapp would be not ready for world domination and you'd risk Mojibake when an enduser attempts to send Unicode-encoded data anyway (e.g. Hebrew, Cyrillic, Chinese, etc).

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Hi BalusC, Thanks for answering! f:view (I tried to place it in my template directly after html as well as h:body) doesn't seem to help: Firebug keep showing "Content-Type: text/html;charset=UTF-8" for the HTTP header.. Did I miss something? Thx – Federico Aug 13 '12 at 07:43
  • Apparently it's been overridden elsewhere. Start debugging at `FaceletViewHandlingStrategy#createResponseWriter()`. I suspect that the `RenderKit` isn't respecting the encoding set by the view. Perhaps RichFaces specific? – BalusC Aug 13 '12 at 09:35
  • I solved as described below, honestly without understanding what the problem was.. As you say also here: http://stackoverflow.com/questions/8609994/post-parameters-using-wrong-encoding-in-jsf-1-2, UTF-8 is default and didn't make sense to use ISO-8859-1. Nevertheless, my beans received Mojibaked german characters. By setting explictly the request's character encoding I solved the problem.. Strange, isn't it? – Federico Aug 13 '12 at 11:41
1

I didn't really understand the problem, but this allowed me to avoid Mojibake while using UTF-8 instead of ISO-8859-1:

public class EncodingFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        if (req.getCharacterEncoding() == null) {
            req.setCharacterEncoding("UTF-8");
        }
        chain.doFilter(req, resp);
    }

    @Override
    public void destroy() {
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
}

Strangely, without this filter I got Mojibakes although I didn't specify any kind of encoding. I also noticed that (filterConfig.getInitParameter("encoding")) in the init() method always returned null.

Federico
  • 561
  • 2
  • 11
  • 32