【发布时间】:2011-08-10 13:06:56
【问题描述】:
是否可以让我自己的 JSP 标签生成的输出更短?例如,定义如下的标签生成 5 行而不是 1 行。是否可以避免这种情况(在标签源中不将所有 5 行加入 1)?
<%@ tag description="link" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ attribute name="href" required="true" type="java.lang.String" %>
<%@ attribute name="label" required="false" type="java.lang.String" %>
<a href="<c:url value="${href}"/>">${not empty label ? label : href}</a>
不是解决方案:
<%@ tag description="standard input" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%@ attribute name="href" required="true" type="java.lang.String" description="address relative to web-app context" %><%@ attribute name="label" required="false" type="java.lang.String" description="link label" %><a href="<c:url value="${href}"/>">${not empty label ? label : href}</a>
【问题讨论】:
-
这不是另一个问题的精确欺骗:如何修剪自定义标签的空白输出而不简单地删除标签中的所有空白?您不能在标签中使用 。
-
正确答案是可以在标签中添加这个:
<%@ tag trimDirectiveWhitespaces="true" %> -
应该重新打开,因为正确答案(见 cmets)与原来的问题不同。