简体   繁体   中英

How to remove white spaces in JSF output?

是否可以配置JSF 2.0以删除XHTML标记之间不必要的空格?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM