【发布时间】:2015-03-29 01:52:36
【问题描述】:
我编写了一个自定义 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>
【问题讨论】:
-
好吧,修复标签的代码。它在哪里?这个标签有什么用?
href="/css/bootstrap.css"怎么了? -
@JBNizet 我是创建标签的新手,所以我不知道如何避免这种情况。如问题中所述,该标签只是打印出
http://example.com。我需要绝对 URL,因为我的网站使用了许多子域,并且我的标签的href属性中包含的空格会破坏样式表链接。 -
@JBNizet 是的,我已经在使用它了,但它只会在我的文档顶部(即在 doctype 声明之前)去除空格,而不是在标签属性中。所以它解决了另一个问题,但不幸的是不是我现在面临的问题。
标签: jsp custom-tags