Java – taglibs and variable declarations generate blank lines at the top of the source page

I declared some taglibs and variables in the JSP page, as follows:

<%@ 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 application and look at the source code, I see a blank new line on the source page:

.







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

I just put a spot to show the empty line Any ideas on how to delete these empty lines?

Solution

You need to tell the jsp servlet to trim the instruction spaces You can add the following entries 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 want to configure it to be server scoped rather than per webapp, refer to the servlet container documentation You didn't tell you which one to use, but in the case of tomcat, it would be an edit / conf / Web XML, add the following entry to the declaration of < servlet > jsp servlet:

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

Your @ page contains a lot of default and repeated confusion It can be simplified as follows:

<%@page pageEncoding="UTF-8"%>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>