从自定义JSP标记输出中剥离空格

时间:2015-01-29 18:01:04

标签: jsp custom-tags

我编写了一个自定义JSP标记,它基本上构建了一个指向我网站完整域名的URL,例如: http://example.com。问题是在我的标记文件中,我有空格来正确格式化我的代码,当我在JSP文件中使用标记时,这些空格都包含在内。

例如,如果我使用我的标签:

<link rel="stylesheet" type="text/css" href="<custom:domainNameUri />${stylesheet}" />

它将在页面的源代码中显示如下:

<link rel="stylesheet" type="text/css" href="



        http://example.com

/css/bootstrap.css" />

如何阻止空格从我的代码输出,以便在使用代码时不会破坏内容?

修改:以下是代码源代码的一部分:

<%@ attribute name="includePort" required="false" type="java.lang.Boolean" description="Whether or not to include the server port in the URI" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<c:set var="domain" value="${pageContext.request.serverName}" />

<%-- Build domain name URI --%>
<c:set var="uri" value="${pageContext.request.scheme}://${domain}" />

<c:choose>
    <c:when test="${includePort == true}">
        <c:out value="${uri}:${pageContext.request.serverPort}" />
    </c:when>

    <c:otherwise>
        <c:out value="${uri}" />
    </c:otherwise>
</c:choose>

1 个答案:

答案 0 :(得分:0)

用它来修剪空间。

<%@ page trimDirectiveWhitespaces="true" %>