简体   繁体   English

为什么这个PrettyTime自定义标记在HTML输出中的“漂亮”日期之前产生11行空白文本?

[英]Why does this PrettyTime custom tag produce 11 lines of blank text before the “pretty” date in the HTML output?

<%@ tag language="java" pageEncoding="utf-8" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ tag import="com.ocpsoft.pretty.time.PrettyTime, java.util.Date"%>
<%@ attribute name="dateParam" required="true" type="java.util.Date" %>

<%
 PrettyTime p = new PrettyTime();
 String prettyDate = p.format(dateParam);
 jspContext.setAttribute("prettyDate", prettyDate.trim());
%>
<c:out value="${prettyDate}"/>

I can't figure out if I'm doing something wrong in this tag. 我无法弄清楚我是否在此标签中做错了什么。

The PrettyTime library is supposed to just print a text version of a data, eg: PrettyTime库应该仅打印数据的文本版本,例如:

10 months ago

But I can't tell why this custom tag produces 11 lines of blank text before the "pretty" date in the HTML output? 但是我不知道为什么这个自定义标记在HTML输出中的“漂亮”日期之前产生11行空白文本

Since Thorbjoern has already answered the cause, I'll only answer the solution since you'd likely to get rid of this annoyance. 由于Thorbjoern已经回答了原因,所以我只回答解决方案,因为您可能会摆脱这种烦恼。

You can configure your servletcontainer to trim whitespace left after processing the scriptlets and taglibs. 您可以将servlet容器配置为在处理scriptlet和taglib之后修剪剩余的空格。 In for example Apache Tomcat, you can do it by opening the /conf/web.xml , heading to the <servlet> definition of the JSP servlet, which look like follows on Tomcat 7 例如,在Apache Tomcat中,您可以打开/conf/web.xml ,转到JSP servlet的<servlet>定义,该定义在Tomcat 7上如下所示

<servlet>
    <servlet-name>jsp</servlet-name>
    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
    <init-param>
        <param-name>fork</param-name>
        <param-value>false</param-value>
    </init-param>
    <init-param>
        <param-name>xpoweredBy</param-name>
        <param-value>false</param-value>
    </init-param>
    <load-on-startup>3</load-on-startup>
</servlet>

add an <init-param> of trimSpaces=true as follows to the <servlet> definition of JSP servlet: 向JSP servlet的<servlet>定义中添加trimSpaces=true<init-param>

    <init-param>
        <param-name>trimSpaces</param-name>
        <param-value>true</param-value>
    </init-param>

Restart Tomcat and this whitespace should be gone. 重新启动Tomcat,这个空白应该消失了。 At least, most of will be gone. 至少,大多数将消失。 You only need to take care that the whitespace which you've introduced yourself is physically removed from the JSP as well. 你只需要要小心,你已经介绍了自己的空格在物理上与JSP移除。

See also the JSP engine HOW-TO . 另请参见JSP引擎HOW-TO Pretty all other servletcontainers have a similar configuration. 几乎所有其他servlet容器都具有类似的配置。 Consult their documentation using the keyword "trim spaces". 使用关键字“修剪空间”查阅其文档。


As to the general approach, I'd suggest to transform that thing into a Java class and make an EL function of it instead. 至于一般方法,我建议将其转换为Java类,并使其具有EL函数。 Death to the scriptlets . 脚本的 死亡

<c:out value="${my:prettyTime(date)}" />

因为当您删除<%...%>时,仍然存在换行符,这些换行符会忠实地复制到生成的输出中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM