0

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?

BraveOtter
  • 114
  • 1
  • 12
  • Does this answer your question? [Unicode input retrieved via PrimeFaces input components become corrupted](https://stackoverflow.com/questions/9634230/unicode-input-retrieved-via-primefaces-input-components-become-corrupted) – Jasper de Vries Nov 22 '22 at 13:12
  • @JasperdeVries I have tried but this is not exactly my problem. With this filter I can change the encoding of the request, but my request encoding is correct. I need to change RESPONSE encoding. – BraveOtter Nov 24 '22 at 08:31
  • You can set the encoding of the response in the filter as well. – Jasper de Vries Nov 24 '22 at 08:34
  • @JasperdeVries Ok I finally got it to work using a filter like the one shown in that question. If you post the answer, I'll mark it as a solution. – BraveOtter Nov 24 '22 at 18:53
  • Just as an FYI OmniFaces provides this filter out of the box: https://showcase.omnifaces.org/filters/CharacterEncodingFilter – Melloware Nov 29 '22 at 14:49

1 Answers1

2

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);
    }

    // ...
}
Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102