Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

taglibs and variable declarations generate empty lines at the top of source page

I have declared some taglibs and variables in my jsp page as below:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<c:set var="site" scope="session" value="${site}"/>
<c:set var="thumb" scope="session" value="${thumbnail}"/>
<c:set var="geoCode" scope="session" value="${geoCode}"/>
...
<fmt:setLocale value="${local}" />
<fmt:setBundle basename="MessagesBundle" />
<!DOCTYPE html>
<head>
    <meta charset="utf-8">
...

when I run the app and then view the source I see blank new lines on top of the source page:

.







<!DOCTYPE html>
<head>
    <meta charset="utf-8">

I just placed a dot to demonstrate the empty lines. Any idea on how I can remove these empty lines?

like image 294
tokhi Avatar asked Nov 20 '13 15:11

tokhi


People also ask

What is a tag library?

A tag library defines a collection of custom actions. The tags can be used directly by developers in manually coding a JSP page, or automatically by Java development tools. A tag library must be portable between different JSP container implementations.

What is Jstl used for?

JSTL stands for JSP Standard Tag Library. JSTL is the standard tag library that provides tags to control the JSP page behavior. JSTL tags can be used for iteration and control statements, internationalization, SQL etc.

What is Taglib URI in JSP?

The taglib directive declares that your JSP page uses a set of custom tags, identifies the location of the library, and provides means for identifying the custom tags in your JSP page. The taglib directive follows the syntax given below − <%@ taglib uri = "uri" prefix = "prefixOfTag" >

What is Jstl full form?

The Jakarta Standard Tag Library (JSTL; formerly JavaServer Pages Standard Tag Library) is a component of the Java EE Web application development platform.


1 Answers

You need to tell the JSP servlet to trim directive whitespaces. You can achieve that by adding the following entry to webapp's web.xml:

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

Or, if you'd like to configure it server-wide instead of on a per-webapp basis, then consult the servletcontainer documentation on the matter. You didn't tell which one you're using, but in case of Tomcat, it'd be a matter of editing its /conf/web.xml to add the following entry to the <servlet> declaration of the JSP servlet:

<init-param>
    <param-name>trimSpaces</param-name>
    <param-value>true</param-value>
</init-param>

Unrelated to the concrete problem, your @page contains a lot of default and repeated mess. It can be simplified as follows:

<%@page pageEncoding="UTF-8"%>
like image 89
BalusC Avatar answered Oct 15 '22 16:10

BalusC