Wednesday August 01, 2007
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.
Posted in Java
at Aug 01 2007, 05:01:25 PM MDT
9 Comments
Search This Site
Recent Entries
- My What's New in Spring 3.1 Presentation
- Twitter's Open Source Summit: Bootstrap 2.0 Edition
- Refreshing AppFuse's UI with Twitter Bootstrap
- 2011 - A Year in Review
- Upgrading AppFuse to Spring Security 3.1 and Spring 3.1
- What have I been working on at Taleo?
- Our Engaging Trip to Paris and Antwerp
- My HTML5 with Play Scala, CoffeeScript and Jade Presentation from Devoxx 2011
- Deploying Java and Play Framework Apps to the Cloud with James Ward
- PhoneGap for Hybrid App Development
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 04:43 AM MDT #
Posted by Ned on August 02, 2007 at 06:06 PM MDT #
Trim filter
JavaScript trim
CSS trim filter
Posted by Dmitry on August 06, 2007 at 04:14 AM MDT #
<c:set var="space" value=" "/> <fmt:message key="see" />${space}<aa href="/here">here</aa>and other, similiar workarounds. Maybe someone has a better solution? Thanks again!Posted by oberon on January 18, 2008 at 03:44 AM MST #
Posted by Al on January 23, 2008 at 03:48 PM MST #
Posted by oberon on January 24, 2008 at 09:46 AM 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 06:52 AM MST #
Posted by lance on March 13, 2010 at 11:40 AM MST #
Posted by Jay on December 20, 2010 at 09:56 AM MST #