4

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

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

3 Comments

In MyFaces Core 2.1.10 I added this feature directly on facelets compiler. See the answer below.
@lu4242: does it take HTML <pre> and <textarea>, CSS white-space and JS element.style.whiteSpace into account?
It removes spaces in the facelets compiler markup directly, but doesn't filter what's the Renderer classes do, which is quite nice because if you need something special you can put the markup on a bean and output it using a <h:outputText .../> and that's it. In my opinion is the best option available so far. Let me know your thoughts and maybe we could improve this feature on MyFaces land.
4

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.

2 Comments

+1 for including the context from the blog post that you referenced. :)
+1 thanks, it works for me. just edit your post to close <faces-config-extension> tag as it may lead someone to confusion
2

I am trying to find simple solution to compress html removing white spaces created after primefaces>jsf>jsp>servlet parsing.

I learned that primefaces can help me with compressing js and css however nothing they can do about HTML because jsf to jsp is happening after primefaces parsing.

JSP specs used to have a directive

 <%@ page trimDirectiveWhitespaces="true" %> 

to do what I need.

Besides, we could handle it in web.xml if needed.

I know that the right way is to configure web server to handle it.

However, jBoss 7.1.1 lost sensitivity to JSP configuration. (Problem started in 7.0 when we had to restart server after each and every JSP change.It got fixed and came back in 7.1. Supposed to be fixed in 7.2 but 7.2 is not out yet.)

In any case, jBoss is fine tool.

My question is if we are loosing functionality going from JSP to JSF.

It should be some sort of tag telling JSF to put trimDirectiveWhitespaces into JSP which it is trying to parse itself into.

I am not liking "filter" solution because it will trim output each time we streaming it out. I rather have it compiled into JSP>Sevlet vs. doing it each time on the way out. Besides, it is custom made (not a standard, not documented etc...).

Still like to know easy way to 'trim white spaces', 'compress html' ... in JSF.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.