Trim Spaces in your JSP's HTML Redux
Since my last post on trimming whitespace in JSPs seems to be a popular topic, I figured it appropriate to note that JSP 2.1 supports a new trimWhitespace directive.
<%@ page trimDirectiveWhitespaces="true" %>
If you're using a Servlet 2.5 XSD, you can also do this in your web.xml:
<jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <trim-directive-whitespaces>true</trim-directive-whitespaces> </jsp-property-group> </jsp-config>
A Servlet 2.5 XSD seems to be the following:
<web-app 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-app_2_5.xsd" metadata-complete="false" version="2.5">
To learn more about this feature, view Summary of New Features in JSP 2.1 Technology and search for "TrimWhiteSpace". It'd be nice if there was anchors in this article for a direct link, but I couldn't find any.
Now I just wish JSP's EL had an xmlEscape="true" flag to escape XML in printed variables. Freemarker supports this.
Hat tip to Kerem and Krishna's Unified Expression Language for JSP and JSF article.
Also, using jspx (JSP XML document) trims white spaces automatically (unfortunately it has some side effects)
I found also jsmin to trim javascripts (http://www.crockford.com/javascript/jsmin.html)
I wrote a filter to use it transparently in my apps (http://jrainbow.newinstance.it/xref/it/newinstance/util/servlet/JSMinFilter.html) but I had to modify jsmin source.
Once I had a client complaining that the html of my app was not "regularly" indented, so... for this kind of clients, an html beautifier is needed
Posted by Luigi R. Viggiano on August 02, 2007 at 10:43 AM MDT #
Posted by Ned on August 03, 2007 at 12:06 AM MDT #
Trim filter
JavaScript trim
CSS trim filter
Posted by Dmitry on August 06, 2007 at 10:14 AM MDT #
Posted by oberon on January 18, 2008 at 09:44 AM MST #
Posted by Al on January 23, 2008 at 09:48 PM MST #
Posted by oberon on January 24, 2008 at 03:46 PM MST #
Hi, I also had this problem and I tried to put (& n b s p) but for some reason the space is now widers bettween two two words separated by this non-brk-space.
Hi ${firstName}&nbsp;{lastName} would result in a "hi Danny FName" where the space is wider than it normally would before the trimspaces change.
Is there someway to control the width of the space? or is there another thing that I am doing wrong here?
Thanks,
- Erez
Posted by Erez on February 11, 2008 at 12:52 PM MST #
Posted by lance on March 13, 2010 at 05:40 PM MST #
Posted by Jay on December 20, 2010 at 03:56 PM MST #
Posted by Ravinder on June 25, 2012 at 02:04 PM MDT #