Is it possible to configure JSF 2.0 to remove unnecessary white spaces between XHTML tags?
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.
This answer comes from my blog:
http://lu4242.blogspot.com/2012/12/html-white-space-compression-for-jsf.html
You can remove those spaces using MyFaces Core version 2.1.10 or upper, adding this in your faces-config.xml:
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1.xsd"
version="2.1">
<faces-config-extension>
<facelets-processing>
<file-extension>.xhtml</file-extension>
<process-as>xhtml</process-as>
<oam-compress-spaces>true</oam-compress-spaces>
</facelets-processing>
</faces-config-extension>
</faces-config>
That's it. Now, facelets compiler will try to reduce or remove spaces/tabs when they are not necessary, following the rules for html white space compression, to avoid change the apperance of the page. In simple words, this means when necessary it replace multiple continuous spaces with just one or remove all of them. It also try to use '\n' characters when possible, to make easier read the page markup once compressed.
Since this optimization is done in facelets compiler, the effort to reduce white spaces is just done once, so all your pages will not impose additional CPU or memory overhead. It's more, it reduce the memory and CPU resources required to render a page, so this can give a little boost to your application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With