yegor256 yegor256 - 4 months ago 18
Java Question

How to remove white spaces in JSF output?

Is it possible to configure JSF 2.0 to remove unnecessary white spaces between XHTML tags?

Answer

No. Facelets cannot distinguish unnecessary whitespace from necessary whitespace. For that it would need to determine individual HTML tags, parse CSS and JS files for any evidence that it is really unnecessary. In case of the HTML <pre> and <textarea> tags, the CSS white-space:pre property and JS element.style.whiteSpace='pre' code, the whitespace is namely significant.

It's simply too expensive and complicated to check that reliably. If your actual concern is network bandwidth, then just turn on gzip compression at server level. How to do that depends on the server used, but on Tomcat for example it's as easy as adding compression="on" to the <Connector> element in /conf/server.xml.

It is however possible to create a Filter which replaces the response writer to trim the whitespace. You can find here an example of such a filter. It only doesn't take CSS/JS into account.

Comments