本文介绍了为什么这个PrettyTime自定义标签在“漂亮"字样之前会产生11行空白文本. HTML输出中的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

<%@ 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.

PrettyTime库应该只打印数据的文本版本,例如:

The PrettyTime library is supposed to just print a text version of a data, e.g.:

10 months ago

但是我无法确定为什么这个自定义标记在HTML输出中的漂亮"日期之前产生 11行空白文本?

But I can't tell why this custom tag produces 11 lines of blank text before the "pretty" date in the HTML output?

推荐答案

由于Thorbjoern已经回答了原因,所以我只回答解决方案,因为您可能会摆脱这种烦恼.

Since Thorbjoern has already answered the cause, I'll only answer the solution since you'd likely to get rid of this annoyance.

您可以将servlet容器配置为在处理scriptlet和taglib之后修剪剩余的空格.例如,在Apache Tomcat中,您可以通过打开/conf/web.xml并转到JSP servlet的<servlet>定义来实现,在Tomcat 7上如下所示

You can configure your servletcontainer to trim whitespace left after processing the scriptlets and taglibs. 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

<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>

在JSP Servlet的<servlet>定义中添加trimSpaces=true<init-param>如下:

add an <init-param> of trimSpaces=true as follows to the <servlet> definition of JSP servlet:

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

重新启动Tomcat,此空白应该消失了.至少,大多数将消失.您只需要注意将您引入的您自己的空白也从JSP中物理删除.

Restart Tomcat and this whitespace should be gone. 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引擎指南.几乎所有其他servlet容器都具有类似的配置.使用关键字修剪空间"查阅他们的文档.

See also the JSP engine HOW-TO. Pretty all other servletcontainers have a similar configuration. Consult their documentation using the keyword "trim spaces".

对于一般方法,我建议将其转换为Java类,并使其具有EL函数. 死亡小脚本.

As to the general approach, I'd suggest to transform that thing into a Java class and make an EL function of it instead. Death to the scriptlets.

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

这篇关于为什么这个PrettyTime自定义标签在“漂亮"字样之前会产生11行空白文本. HTML输出中的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 00:34