您可以使用指令 trimDirectiveWhitespaces
<%@ page trimDirectiveWhitespaces="true"%>
这应该会删除动作、scriptlet 和指令之间的空格。
这是我在 Glassfish 4.0 上获得的示例:
在jsp中带有
<%@ page trimDirectiveWhitespaces="true"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<%="1111111111"%>
<%="2222222222"%>
<c:out value="c:out"></c:out>
<c:out value="LOOP"></c:out>
<c:forEach begin="1" end="5" step="1" var="y">
${'qqqqq'} ${'RRR'}
<c:out value="c:out"></c:out>
<% out.println("InTheLoop"); %>
</c:forEach>
</tr>
</table>
</body>
</html>
浏览器收到的源代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
11111111112222222222c:outLOOPqqqqqRRRc:outInTheLoop
qqqqqRRRc:outInTheLoop
qqqqqRRRc:outInTheLoop
qqqqqRRRc:outInTheLoop
qqqqqRRRc:outInTheLoop
</tr>
</table>
</body>
</html>
与上面相同的 JSP 代码,但 带有
或者完全没有 trimDirectiveWhitespaces
生成浏览器中收到的以下代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
1111111111
2222222222
c:out
LOOP
qqqqq RRR
c:out
InTheLoop
qqqqq RRR
c:out
InTheLoop
qqqqq RRR
c:out
InTheLoop
qqqqq RRR
c:out
InTheLoop
qqqqq RRR
c:out
InTheLoop
</tr>
</table>
</body>
</html>
因此使用 trimDirectiveWhitespace="true":
- 您可以删除操作、脚本、表达式、指令之间的空格。
- 但您不能删除其他 html 标记之间的空格