Wednesday, 16 June 2010

trimDirectiveWhitespaces with JSTL

While using JSP I always use JSTL to avoid any use of java on the JSP itself. To get a quick world cup 2010 tipping competition up and running I found that my response pages using JSTL contained just way to much white space. Luckily in the JSP 2.1 world we can use the page directive trimDirectiveWhitespaces to easily remove that unwanted white spaces. Basically it's done as follows


<%@ page
contentType="text/html;charset=windows-1252"
trimDirectiveWhitespaces="true" %>

So to see how well this works simply use the code below in JDeveloper 10.1.3 (JSP 2.0) and then in JDeveloper 11g (JSP 2.1) and view the source of the page after running it.

Note: When running this code in JDeveloper 10.1.3 you will have to remove trimDirectiveWhitespaces="true" as it does not support that.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page contentType="text/html;charset=windows-1252" trimDirectiveWhitespaces="true" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
<title>JSP 2.1 - trimDirectiveWhitespaces JSTL demo</title>
</head>
<body>
<h2>JSP 2.1 - trimDirectiveWhitespaces JSTL demo</h2>

<c:forEach var="row" begin="1" end="5">
<c:choose>
<c:when test="${row == 1}">
<font color="green">
<c:out value="At row 1"/>
</font>
<br />
</c:when>
<c:otherwise>
<c:choose>
<c:when test="${row != 1}">
<font color="Red">
<c:out value="Not at row 1"/>
</font>
</c:when>
<c:otherwise>
<c:out value="Should never get here"/>
</c:otherwise>
</c:choose>
<br />
</c:otherwise>
</c:choose>
</c:forEach>

</body>
</html>

No comments: