How to remove spaces in JSF output?

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

+4
source share
3 answers

No. Persons cannot distinguish unnecessary spaces from necessary spaces. To do this, he will need to identify individual HTML tags, analyze CSS and JS files for any evidence that this is not really necessary. In the case of HTML <pre> and <textarea> tags, CSS properties white-space:pre and JS element.style.whiteSpace='pre' , spaces are significant.

It is simply too expensive and difficult to verify that it is reliable. If your actual problem is with network bandwidth, then just enable gzip compression at the server level. How to do this depends on the server used, but on Tomcat, for example, it is as simple as adding compression="on" to the <Connector> element in /conf/server.xml .

However, it is possible to create a Filter that replaces the author of the answer to trim the spaces. You can find an example of such a filter here. This does not take into account CSS / JS.

+7
source

This answer comes from my blog:

http://lu4242.blogspot.com/2012/12/html-white-space-compression-for-jsf.html


You can remove these spaces with MyFaces Core version 2.1.10 or higher by adding this to 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> 

What is it. Now, the facelets compiler will try to reduce or remove spaces / tabs when they are not needed, following the rules for compressing html white space to avoid changing the page's ability. In simple words, this means that, if necessary, it replaces several continuous spaces with one or removes all of them. He also tries to use the characters \ n when possible to make it easier to read page layout after compression.

Since this optimization is performed in the facelets compiler, efforts to reduce spaces are performed only once, so all your pages will not impose additional overhead on the CPU or memory. Moreover, it reduces the memory and processor resources needed to render the page, so this may push your application a bit.

+4
source

I am trying to find a simple solution to compress html removing white spaces created after parsing cerfs> jsf> jsp> servlets.

I found out that with the help of compression js and css can do nothing with HTML, because jsf in jsp happens after parsing partitions.

JSP specifications have a directive

  <%@ page trimDirectiveWhitespaces="true" %> 

do what i need.

In addition, we could process it in web.xml, if necessary.

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

However, jBoss 7.1.1 has lost sensitivity to the JSP configuration. (The problem started in 7.0, when we had to restart the server after each JSP change. It was fixed and returned to 7.1. Suppose that it was fixed in 7.2, but 7.2 has not yet been released.)

In any case, jBoss is a great tool.

My question is that we are losing functionality from JSP to JSF.

It needs to be some kind of tag saying JSF to put trimDirectiveWhitespaces in the JSP that it is trying to parse.

I don’t like the β€œfilter” solution, because it will clip the output every time we unload it. Rather, I compiled it in JSP> Sevlet vs. do it every time you exit. In addition, it is custom-made (not standard, not documented, etc.).

I would also like to know a simple way to "crop white spaces", "compress html" ... in JSF.

+2
source

Source: https://habr.com/ru/post/1341603/


All Articles