BalusC's solution to calling UIComponent#encodeAll() usually works, but when using utf-8 encoding, I had a problem with Unicode characters. All non-ascii characters in ajax response were corrupted after I changed the current response script to a script.
Instead of changing the response script in the current context obtained by FacesContext.getCurrentInstance () , I created a wrapper on top of the current context by expanding FacesContextWrapper so that the original context remains unchanged:
StringWriter writer = new StringWriter(); FacesContext context = new FacesContextWrapper() { private ResponseWriter internalWriter = getWrapped() .getRenderKit().createResponseWriter(writer, "text/html", "UTF-8"); @Override public FacesContext getWrapped() { return FacesContext.getCurrentInstance(); } @Override public ResponseWriter getResponseWriter() { return internalWriter; } }; component.encodeAll(context); String output = writer.toString();
source share