2 Replies Latest reply on Mar 7, 2012 6:43 PM by Cristiano Moraes

    Encoding issue in autocomplete

    Cristiano Moraes Newbie

      Hi,

       

      I'm trying to include an autocomplete component in my application, but I'm having some trouble with that.

       

      If I type some accent character, as 'ção' for example ,in autocomplete field, in the server side method, I receive weird character like "ç".

       

      I believe that is a client side issue, because it works fine in Firefox. At other hand, in Chrome and IE8, it fails.

       

      Here the code that I'm using to test this:

       

      autocompleteTest.xhtml

      <?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"
            xmlns:a4j="http://richfaces.org/a4j"
            xmlns:rich="http://richfaces.org/rich">
      
      
      <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
      
      <f:view contentType="text/html" encoding="UTF-8">
      
      
      <h:head>
          <title>teste</title>
      </h:head>
      
      
      <h:body>
          <h:form acceptcharset="UTF-8">
                      <rich:autocomplete mode="ajax" minChars="1"
                            autofill="false"  autocompleteMethod="#{autocompleteTest.autocomplete}"                      
                            id="myAutocomplete" var="modeler"/>
          </h:form>
      </h:body>
      
      
      </f:view>
      </html>
      
      

       

       

      AutocompleteTest.java

      package teste;
      
      
      import java.io.BufferedReader;
      import java.io.InputStreamReader;
      import java.util.ArrayList;
      import java.util.List;
      
      
      import javax.annotation.PostConstruct;
      import javax.faces.bean.ManagedBean;
      import javax.faces.bean.RequestScoped;
      
      
      @ManagedBean
      @RequestScoped
      public class AutocompleteTest {
          private List<String> lst;
      
          public AutocompleteTest() { }
      
          @PostConstruct
          public void init() {
                    try {
                                    lst = new ArrayList<String>();
                                    BufferedReader in = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/resource/CountryList.txt")));
                                    String line;
                                    while((line = in.readLine())!=null) {
                                              lst.add(line);
                                    }
                          } catch (Exception e) {
                                    e.printStackTrace();
                          }
          }
      
          public List<String> autocomplete(String prefix) {
                    System.out.println(prefix);
              ArrayList<String> result = new ArrayList<String>();
              if (prefix!=null && !prefix.isEmpty()){
                  for (String s : lst) {
                      if (s.toLowerCase().indexOf(prefix.toLowerCase()) == 0) {
                          result.add(s);
                      }
                  }
              }
      
      
              return result;
          }
      
      }
      
      

       

      I'm using Glassfish3.

       

      I thanks in advance for any help.

       

      Cristiano

        • 1. Re: Encoding issue in autocomplete
          Ronan Silva Newbie

          I'm with same problem...

           

          How do you resolved this problem???

           

          Thank's!!!

          • 2. Re: Encoding issue in autocomplete
            Cristiano Moraes Newbie

            Hi Ronan,

             

            It wasn't a Richfaces problem after all, but a JSF issue.

             

            I fixed this by creating a filter to set any request for UTF-8 encode. Here the code:

             

            public class FilterApp implements Filter{

             

                public FilterApp() {}

               

                @Override

                public void destroy() {}

             

                @Override

                public void doFilter(ServletRequest request, ServletResponse response,

                        FilterChain chain) throws IOException, ServletException {

                    request.setCharacterEncoding("utf-8"); 

                    chain.doFilter(request, response); 

                   

                }

             

                @Override

                public void init(FilterConfig filterConfig) throws ServletException {}

             

            }

             

             

            And then add these lines in web.xml

             


            <filter>


            <display-name>FiltroApp</display-name>


            <filter-name>FiltroApp</filter-name>


            <filter-class>[your_package].FilterApp</filter-class>

            </filter>

            <filter-mapping>


            <filter-name>FiltroApp</filter-name>


            <url-pattern>/*</url-pattern>


            <dispatcher>REQUEST</dispatcher>


            <dispatcher>FORWARD</dispatcher>

            </filter-mapping>

             

             

            Hope this helps you.

             

            Regards.