Search code examples
jsfservletsprimefacesweblogicprimefaces-datatable

Why Primefaces change encoding of the response?


I have a webapp that uses Primefaces 4 (I cannot update it, it does not deppend on me) with a datatable with a column that sometimes has alphas (the greek letter: α).

To load this datatable, I use a button of a form with some filters. When I click on that button, the backend sends me the response using ISO-8859-15 encoding. That is a problem because with this encoding alphas are represented as a question mark (?) but, when I use the pagination buttons to go to the second page, I receive the response using UTF-8 encoding and alphas are correctly represented, even I can go back to the first page, and I receive the response using UTF-8.

I was able to solve this issue on my local testing Tomcat server changing encoding in the first line of the xhtml file:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>

But this is not working in the production server that uses Weblogic 12c. The data of the datatable came from a lazydatamodel class and following it with the debugger it does exactly the same with both buttons, except for the page obviously. But I leave here the code of the load method of this lazydatamodel class:

private List<ListElement> data;

@Autowired
private transient CamFacade camFacade;

public List<ListElement> load(int first, int pageSize, String sortField,
        SortOrder sortOrder, Map<String, String> filters) {

        Map<String, Object> params = new HashMap<String, Object>();
        params.putAll(filters);
        params.put("first", first);
        params.put("pageSize", pageSize);
        if(sortField == null){
            params.put("orderBy", params.get("orderBy"));
            params.put("order", params.get("order"));
        }else{
            params.put("orderBy", sortField);
            params.put("order", sortOrder.name());
        }


        int count  = camFacade.countByParams(params);
        if (count > 0 ) {
            data = camFacade.findByParams(params);
        } else {
            data = new ArrayList<ListElement>();
        }

        return data;
}

EDIT:

I also tried this solution: Unicode input retrieved via PrimeFaces input components become corrupted

But it does not work for me. If I am not misunderstanding it, with this code I change the request encoding, but actually my request is ok, it is UTF-8. I need to change the RESPONSE encoding.

Any idea how to solve this problem?


Solution

  • You can add a Filter where you set the character encoding on both the request and the response to UTF-8:

    @WebFilter("/*")
    public class CharacterEncodingFilter implements Filter {
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
            request.setCharacterEncoding(StandardCharsets.UTF_8.name());
            response.setCharacterEncoding(StandardCharsets.UTF_8.name());
            chain.doFilter(request, response);
        }
    
        // ...
    }