动态内容中的嵌入式自定义标签(嵌套标签)未呈现

时间:2020-03-05 18:49:12  来源:igfitidea点击:

动态内容中的嵌入式自定义标签(嵌套标签)无法呈现。

我有一个页面,它从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例如,在上面的示例中,如果我在重要的Notice.getMessage()字符串中有一个自定义标签:

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

重要的通知可以很好地显示出来,但是不会处理quote标记,只需将其插入字符串中并作为纯文本/ 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>

我知道这与处理器和预处理器有关,但是我不确定如何使它起作用。

解决方案

回答

只是使用

<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转换器对其进行解析。

我们可以在这种情况下使用regexp,或者尝试以这样的方式重新设计代码,使其具有以下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>

我应该和regexp一起去。