我编写了一个自定义 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>