If you're using a Servlet 2.5 XSD, you can also do this in your web.">
Matt RaibleMatt Raible is a Web Developer and Java Champion. Connect with him on LinkedIn.

The Angular Mini-Book The Angular Mini-Book is a guide to getting started with Angular. You'll learn how to develop a bare-bones application, test it, and deploy it. Then you'll move on to adding Bootstrap, Angular Material, continuous integration, and authentication.

Spring Boot is a popular framework for building REST APIs. You'll learn how to integrate Angular with Spring Boot and use security best practices like HTTPS and a content security policy.

For book updates, follow @angular_book on Twitter.

The JHipster Mini-Book The JHipster Mini-Book is a guide to getting started with hip technologies today: Angular, Bootstrap, and Spring Boot. All of these frameworks are wrapped up in an easy-to-use project called JHipster.

This book shows you how to build an app with JHipster, and guides you through the plethora of tools, techniques and options you can use. Furthermore, it explains the UI and API building blocks so you understand the underpinnings of your great application.

For book updates, follow @jhipster-book on Twitter.

10+ YEARS


Over 10 years ago, I wrote my first blog post. Since then, I've authored books, had kids, traveled the world, found Trish and blogged about it all.

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 10 Comments
Comments:

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 #

Luigi: an HTML beautifier or a big stick. (I'd prefer the latter).

Posted by Ned on August 03, 2007 at 12:06 AM MDT #

There are some components from JSOS:

Trim filter
JavaScript trim
CSS trim filter

Posted by Dmitry on August 06, 2007 at 10:14 AM MDT #

Thanks for your interesting posts on the whitespace topic! Unfortunately, removing whitespace also bears some problems:
<fmt:message key="see" /> <aa href="/here">here</aa>
Here the space between the message and the link is crucial, but is removed, too. Do you have an idea, how to preserve it? An ugly solution I found is
<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 09:44 AM MST #

Can't you just do <fmt:message key="see" />&nbsp;<aa href="/here">here</aa>

Posted by Al on January 23, 2008 at 09:48 PM MST #

Sure. But that avoids possibly neccessary linebreaks at that position or adds too much space (if I place another space between &nbsp; and <aa...> to circumvent this.) The solution I currently use is to insert a <c:out value=" "/> ... which is a bit ugly. ;-)

Posted by oberon on January 24, 2008 at 03:46 PM MST #

Hi, I also had this problem and I tried to put &nbsp; (& 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}&amp;nbsp;{lastName} would result in a "hi Danny &nbsp;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 #

Is the script on the top can be use to ordinary asp or php page?

Posted by lance on March 13, 2010 at 05:40 PM MST #

How can I get eclipse to stop flagging trimDirectiveWhitespaces as a warning in my JSP pages?

Posted by Jay on December 20, 2010 at 03:56 PM MST #

Removing unnecessary whitespace in a J2EE Application in WebSphere ?

Posted by Ravinder on June 25, 2012 at 02:04 PM MDT #

Post a Comment:
  • HTML Syntax: Allowed