Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PrimeFaces 3.3 <p:datatable> filtering fails with UTF8 [duplicate]

Tags:

jsf

primefaces

I have a PrimeFaces 3.3 / JSF application which I deploy to JBoss AS 7.1. To display my data I use a p:dataTable with some filtering headers. Here's the code (after narrowing down the sources):

<p:outputPanel id="custDataTable">            
        <p:dataTable var="item" value="#{customerController.items}" rowKey="#{item.id}"
                        selection="#{customerController.current}" selectionMode="single" id="customersTable">
            <p:column headerText="Surname" sortBy="#{item.surname}" filterBy="#{item.surname}" id="surname">  
                #{item.surname}
            </p:column>  
            <p:column headerText="Age" sortBy="#{item.age}" filterBy="#{item.age}" id="age" styleClass="age">
                #{item.age}
            </p:column>
            <p:column headerText="&nbsp;">
                <p:spacer width="20" height="0" />
                <p:commandButton update=":custForm" ajax="false" action="#{customerController.prepareEdit}" value="edit">
                    <f:setPropertyActionListener value="#{item}" target="#{customerController.current}" />
                </p:commandButton>
            </p:column>
        </p:dataTable>  
</p:outputPanel>

PrimeFaces p:dataTable filtering on the numeric Age column always works but on the Surname column a strange behaviour arises. When the backing bean's items instance variable has elements with ASCII data in the surname, then filtering works. But when UTF8 data are present, then the filtering works only partially:

[1] I can type UTF8 characters of my locale in the column header field and the results are indeed filtered (that's the part that works).

[2] The backing bean's current instance variable is always null. I.e. the binding:

selection="#{customerController.current}"

doesn't seem to be working. I have added some logging in the CustomerController::prepareEdit method and the value is set to null when the edit p:commandButton is pressed. As a result, I can't edit an instance filtered on the basis of the surname column (when UTF8 data are present). However the same instance, with the same UTF8 data can be edited when I filter on the numeric age column, or when I don't filter at all.

To tackle the problem I tried registering a character encoding filter:

public class CharacterEncodingFilter implements Filter {

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

and registered it in my web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app ...
...
  <filter>                                                                                                                                           
      <filter-name>Character Encoding Filter</filter-name>                                                                                           
      <filter-class>mp.util.CharacterEncodingFilter</filter-class>                                                                                   
  </filter>      
</web-app>

but that didn't do the trick either.

like image 635
Marcus Junius Brutus Avatar asked Jul 14 '12 20:07

Marcus Junius Brutus


1 Answers

You need to ensure that the request character encoding is been set to UTF-8. You can do that with a servlet filter which is been mapped on an URL pattern covering the requests of interest. E.g. /* or just on the servlet name of the FacesServlet.

@WebFilter("/*")
public class CharacterEncodingFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        chain.doFilter(request, response);
    }

    // ...
}

See also:

  • Unicode input retrieved via PrimeFaces input components become corrupted
like image 105
BalusC Avatar answered Nov 15 '22 08:11

BalusC