How to remove line breaks after JSP tags?

java, jsp, spring-mvc

Solution

Copied from Strip whitespace from jsp output There is a trimWhiteSpaces directive that should accomplish this,

In your JSP:

<%@ page trimDirectiveWhitespaces="true" %>

Or in the jsp-config section your web.xml (Note that this works starting from servlet specification 2.5.):

<jsp-config>
  <jsp-property-group>
    <url-pattern>*.jsp</url-pattern>
    <trim-directive-whitespaces>true</trim-directive-whitespaces>
  </jsp-property-group>
</jsp-config>

Unfortunately if you have a required space it might also need strip that, so you may need a non-breaking space in some locations.

Problem

How to remove line breaks after JSP tags? For example, if in JSP page has code with tag like this ``` <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>${pageTitle}</title> </head> ``` Than in output result will be one line break at line one. ``` !HERE_LINE_BREAK! <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title></title> </head> ``` How I can remove unneeded line break at the first line? JSP processed with Spring Web MVC. Thanks.

Original source

Related problems