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?

有帮助吗?

解决方案

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"%>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top