动态内容中嵌入的自定义标签(嵌套标签)不呈现。
我有一个页面,它从 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>
这个标签应该输出一个像这样的盒子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>
我知道这与处理器和预处理器有关,但我不确定如何使其工作。
最佳答案
刚刚使用
<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>
我会选择正则表达式。
关于java - 动态内容中嵌入的自定义标签(嵌套标签)不呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49267/