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 8 Comments
Search This Site
Recent Entries
- Fantastic Fun in Jackson Hole
- How We Hired a Team of 10 in 2 Months
- Jack's Skiing Like A Madman!
- My Future of Web Frameworks Presentation
- My Guest Room Remodel is finished!
- Web Application Testing with Selenium by Jason Huggins
- Web Application Testing with Selenium at Agile Denver next Monday
- 17" MacBook Pro Stolen from Living Room
- Using JRebel with IntelliJ IDEA on OS X
- Reviews for Grails: A Quick-Start Guide and Kanban and Scrum
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 02:44 AM MST #
Posted by Al on January 23, 2008 at 02:48 PM MST #
Posted by oberon on January 24, 2008 at 08: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 05:52 AM MST #
Posted by lance on March 13, 2010 at 10:40 AM MST #