【问题标题】:Embedded custom-tag in dynamic content (nested tag) not rendering动态内容中的嵌入自定义标签(嵌套标签)不呈现
【发布时间】:2010-09-08 03:14:30
【问题描述】:

在动态内容(嵌套标签)中嵌入自定义标签不呈现。

我有一个页面,它从 javabean 中提取动态内容并将对象列表传递给自定义标记以处理成 html。在每个对象中都有一堆要输出的 html,其中包含我希望也呈现的第二个自定义标签。问题是标签调用被渲染为明文。

举个例子可能会更好。

1 从数据库中提取信息并通过 javabean 返回到页面。将此信息发送到自定义标签以进行输出。

<jsp:useBean id="ImportantNoticeBean" scope="page" class="com.mysite.beans.ImportantNoticeProcessBean"/>  <%-- Declare the bean --%>
<c:forEach var="noticeBean" items="${ImportantNoticeBean.importantNotices}"> <%-- Get the info --%>
    <mysite:notice importantNotice="${noticeBean}"/> <%-- give it to the tag for processing --%>
</c:forEach>

这个标签应该像这样输出一个box div

*SNIP* class for custom tag def and method setup etc
out.println("<div class=\"importantNotice\">");
out.println("   " + importantNotice.getMessage());
out.println("   <div class=\"importantnoticedates\">Posted: " + importantNotice.getDateFrom() + " End: " + importantNotice.getDateTo()</div>");
out.println("   <div class=\"noticeAuthor\">- " + importantNotice.getAuthor() + "</div>");
out.println("</div>");
*SNIP*

这渲染得很好,符合预期

<div class="importantNotice">
    <p>This is a very important message. Everyone should pay attenton to it.</p>
    <div class="importantnoticedates">Posted: 2008-09-08 End: 2008-09-08</div>
    <div class="noticeAuthor">- The author</div>
</div>

2 例如,在上面的示例中,我要在 importantNotice.getMessage() 字符串中有一个自定义标签:

*SNIP* "This is a very important message. Everyone should pay attenton to it. <mysite:quote author="Some Guy">Quote this</mysite:quote>" *SNIP*

重要提示渲染正常,但引号标签不会被处理,而是简单地插入到字符串中并作为纯文本/html标签放置。

<div class="importantNotice">
    <p>This is a very important message. Everyone should pay attenton to it. <mysite:quote author="Some Guy">Quote this</mysite:quote></p>
    <div class="importantnoticedates">Posted: 2008-09-08 End: 2008-09-08</div>
    <div class="noticeAuthor">- The author</div>
</div>

而不是

<div class="importantNotice">
    <p>This is a very important message. Everyone should pay attenton to it. <div class="quote">Quote this <span class="authorofquote">Some Guy</span></div></p>    // or wahtever I choose as the output
    <div class="importantnoticedates">Posted: 2008-09-08 End: 2008-09-08</div>
    <div class="noticeAuthor">- The author</div>
</div>

我知道这与处理器和预处理器有关,但我不确定如何进行这项工作。

【问题讨论】:

    标签: java jsp jstl custom-tag


    【解决方案1】:

    只是使用

    <bodycontent>JSP</bodycontent>
    

    还不够。你应该这样做

    JspFragment body = getJspBody(); 
    StringWriter stringWriter = new StringWriter(); 
    StringBuffer buff = stringWriter.getBuffer(); 
    buff.append("<h1>"); 
    body.invoke(stringWriter); 
    buff.append("</h1>"); 
    out.println(stringWriter);
    

    获取渲染的内部标签(示例是 SimpleTag doTag 方法)。

    但是,在问题的代码中,我看到内部标记来自一个未呈现为 JSP 一部分的字符串,而只是一些随机字符串。我认为您不能强制 JSP 翻译器对其进行解析。

    您可以在您的情况下使用正则表达式,或者尝试重新设计您的代码以拥有这样的 jsp:

    <jsp:useBean id="ImportantNoticeBean" scope="page class="com.mysite.beans.ImportantNoticeProcessBean"/>
    <c:forEach var="noticeBean" items="${ImportantNoticeBean.importantNotices}">
        <mysite:notice importantNotice="${noticeBean}">
            <mysite:quote author="Some Guy">Quote this</mysite:quote>
            <mysite:messagebody author="Some Guy" />
        </mysite:notice>
    </c:forEach>
    

    我应该使用正则表达式。

    【讨论】:

      【解决方案2】:

      我倾向于更改“标记的架构”,因为您希望获得的数据不应该是类内部的标记,因为它是为页面设计的“标记”(虽然在默默无闻中可以获得JSP Servlet引擎的评估程序线程)。

      在标准过程中您可能会发现更好和更多的是使用带有BodyTagSupport 类扩展的“合作标签”并在doStartTag() 方法中返回EVAL_BODY_BUFFERED 以重复处理主体和/或 对象共享,例如将检索到的数据存储在会话的应用程序层次结构中或用户的会话中。

      更多信息请参见oracle j2ee custom tags tutorial

      【讨论】:

        猜你喜欢
        • 2018-01-03
        • 1970-01-01
        • 1970-01-01
        • 2016-08-25
        • 2011-12-15
        • 2019-01-23
        • 2014-12-26
        • 2013-10-14
        • 1970-01-01
        相关资源
        最近更新 更多