14 Replies Latest reply on Jul 30, 2009 11:22 AM by jkronegg

    JSF facelets and Seam

    hanabi

      just found out when learning Seam that my JSF/Facelets form  is sent to browser with ISO encoding in response header. When I Input Japanese characters into the form the result is browken - I see characters escaped as #&63644. I tried to find how can I control what encoding is used by default by JBoss, and failed... Am I missing smth?

        • 1. Re: JSF facelets and Seam
          schlegel

          Did you try utf-8...



          <?xml version="1.0" encoding="utf-8"?>
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core">
          <head>
              <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8"/>
              <meta http-equiv="Content-Script-Type" content="text/javascript"/>
              <meta http-equiv="Content-Style-Type" content="text/css"/>
              <meta http-equiv="expires" content="0"/>
              <meta http-equiv="cache-control" content="no-cache, no-store"/>
              <meta http-equiv="pragma" content="no-cache"/>


          • 2. Re: JSF facelets and Seam
            hanabi

            yes
            and I get
            &#65345;&#65365;&#12502;&#12521;&#12531;&#12489;&#12398;&#20140;&#12475;&#12521;&#35069;&#25658;&#24111;&#38651;&#35441;</td>


            instead of Japanese
            and still
            Content-Type: application/xhtml+xml;charset=ISO-8859-1
            in response header from server

            • 3. Re: JSF facelets and Seam

              I have an application that uses cyrillics (also non latin alphabet, so the problem is similar).


              Note the problems are often also (may sound strange to some but it's actually true) vendor specific, i.e. under one vendor/server it may work and under another you you need a different configuration.


              I had some problems with an application working perfectly and 'according to design' under Tomcat and then suddenly giving encoding problems when switching to Resin.


              This is what I have:


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



              Note, it's not xhtml (not sure if it will help, I actually took it over from plain html).


              I do remember I had problems with regular html forms request which I solved with the following:



              <form action="rus_win/destination.jsp" method="get" accept-charset="UTF-8" lang="ru">



              You might also try:



              <html lang="<YOUR LANGUAGE CODE>">




              But TBH I would really start from checking the default codepage of your server...

              • 4. Re: JSF facelets and Seam

                Another tip, if you can use resources (from resource files or database)  - use them :)

                • 5. Re: JSF facelets and Seam
                  hanabi

                  no. i cannot use resources. the form data is comming from user not from server

                  • 6. Re: JSF facelets and Seam
                    hanabi

                    still pages are arriving with


                    Content-Type: text/html;charset=ISO-8859-1

                    • 7. Re: JSF facelets and Seam
                      gimco

                      Try using a filter in your web.xml:



                           <filter>
                                <filter-name>UTF8 Filter</filter-name>
                                <filter-class>com.foo.UTF8Filter</filter-class>
                           </filter>
                           <filter-mapping>
                                <filter-name>UTF8 Filter</filter-name>
                                <url-pattern>*.jsf</url-pattern>
                           </filter-mapping>



                      This filter must be the first.


                      The UTF8Filter is this simple class:


                      public class UTF8Filter implements Filter {
                      
                           public void destroy() {
                           }
                      
                           public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
                                     ServletException {
                                request.setCharacterEncoding("UTF-8");
                                chain.doFilter(request, response);
                           }
                      
                           public void init(FilterConfig arg0) throws ServletException {
                           }
                      
                      }



                      It's the only way that I had success.

                      • 8. Re: JSF facelets and Seam
                        hanabi

                        that's exactly what i did. Found this solution couple of days ago on another forum. Thanx anyway

                        • 9. Re: JSF facelets and Seam
                          zaya

                          Or just add


                          <web:character-encoding-filter encoding="UTF-8" override-client="true"/>



                          in your components.xml file

                          • 10. Re: JSF facelets and Seam
                            hanabi

                            no, this is not working

                            • 11. Re: JSF facelets and Seam
                              angelolloqui

                                   <filter>
                                        <filter-name>UTF8 Filter</filter-name>
                                        <filter-class>com.foo.UTF8Filter</filter-class>
                                   </filter>
                                   <filter-mapping>
                                        <filter-name>UTF8 Filter</filter-name>
                                        <url-pattern>*.jsf</url-pattern>
                                   </filter-mapping>




                              public class UTF8Filter implements Filter {
                              
                                   public void destroy() {
                                   }
                              
                                   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
                                             ServletException {
                                        request.setCharacterEncoding("UTF-8");
                                        chain.doFilter(request, response);
                                   }
                              
                                   public void init(FilterConfig arg0) throws ServletException {
                                   }
                              
                              }




                              Thank you very much!!


                              This way finally works for me too!! with spanish chracters!!


                              I have spent a lot of time with this issue since

                              <web:character-encoding-filter encoding="UTF-8" override-client="true"/>

                              doesn't work.


                              • 12. Re: JSF facelets and Seam
                                rave

                                works if you replace *.jfs by *.seam:


                                <filter-mapping>
                                      <filter-name>UTF8 Filter</filter-name>
                                      <url-pattern>*.seam</url-pattern>
                                </filter-mapping>
                                


                                • 13. Re: JSF facelets and Seam
                                  damianharvey.damianharvey.gmail.com

                                  I also couldn't get the <web:character-encoding-filter> working (using Seam 2.0.2.SP1).


                                  I've placed breakPoints in CharacterEncodingFilter and can see that the request encoding is set to UTF-8 however the value is still not being set correctly when it reaches the model. If I use the standard filter as above (ie. defined in web.xml) then it works.


                                  Any thoughts? Anywhere else that I can look?


                                  I'll test on later versions when I get some time this week and report back. Can anyone confirm that this works on 2.0.2?


                                  Thanks,


                                  Damian.

                                  • 14. Re: JSF facelets and Seam
                                    jkronegg

                                    I had the similar problem. I wanted to write accented characters in european language (e.g. french) in XHTML files and also allow accented characters in HTML forms. The problem was that encoding problems occurs not in standard forms but only in Seam-generated EntityList.xhtml files which uses redirect with page parameters.


                                    There is some things to know about Seam when dealing with encoding of page parameters:



                                    1. Seam Manager's default encoding for URLs (and so for page parameters specified in the page.xml file) is UTF-8.

                                    2. when submitting a form, a POST query with parameters is done, with a redirect to a GET page which does not specify the charset, so the URL page parameters is encoded using the Seam's default encoding (UTF-8 as defined in 1), which may introduce bad characters.



                                    This is how I solved the problem:



                                    • In order to allow accented characters in european languages in the source, I added on the top of my XHTML page:
                                      <?xml version="1.0" encoding="ISO-8859-1"?>


                                    • In order to get URLs and page parameters encoded with the correct charset, I added in components.xml:
                                      <core:manager uri-encoding="ISO-8859-1" ...>


                                    • I did not use <web:character-encoding-filter encoding="UTF-8" override-client="true"/> in components.xml because it worked without it



                                    Note: requires Seam 2.1.0 or higher (maybe you upgraded since 2008).


                                    References: