嵌入式自定义标记在动态内容(嵌套标记)中无法呈现

时间:2021-11-14 08:36:55

Embedded custom-tag in dynamic content (nested tag) not rendering.

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

I have a page that pulls dynamic content from a javabean and passes the list of objects to a custom tag for processing into html. Within each object is a bunch of html to be output that contains a second custom tag that I would like to also be rendered. The problem is that the tag invocation is rendered as plaintext.

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

An example might serve me better.

一个例子可能对我更好。

1 Pull information from a database and return it to the page via a javabean. Send this info to a custom tag for outputting.

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>

this tag should output a box div like so

这个标签应该像这样输出一个盒子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*

This renders fine and as expected

这很好,并且符合预期

<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 If, in the above example, for instance, I were to have a custom tag in the importantNotice.getMessage() String:

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*

The important notice renders fine but the quote tag will not be processed and simply inserted into the string and put as plain text/html tag.

重要的通知呈现得很好,但引号标签不会被处理,只是插入到字符串中并作为纯文本/ 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>

Rather than

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

I know this has to do with processors and pre-processors but I am not to sure about how to make this work.

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

2 个解决方案

#1


1  

Just using

<bodycontent>JSP</bodycontent>

is not enough. You should do soimething like

是不足够的。你应该这样做

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

to get inner tags rendered (example is for SimpleTag doTag method).

获取内部标记(示例适用于SimpleTag doTag方法)。

However, in the question's code I see that inner tag is comming from a string which is not rendered as a part of JSP, but just some random string. I do not think you can force JSP translator to parse it.

但是,在问题的代码中,我看到内部标记来自一个字符串,该字符串不是作为JSP的一部分呈现,而只是一些随机字符串。我认为你不能强迫JSP翻译解析它。

You can use regexp in your case or try to redesign your code in a way to have a jsp like this:

您可以在您的情况下使用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>

I whould go with regexp.

我应该用正则表达式。

#2


0  

I would be inclined to change the "architecture of your tagging" in that the data you wish to achieve should not be by tag on the inside of the class as it is "markup" designed for a page(though in obscurity it is possible to get the evaluating program thread of the JSP Servlet engine).

我倾向于改变“标记的体系结构”,因为你希望实现的数据不应该在类的内部标记,因为它是为页面设计的“标记”(虽然在默默无闻中它是可能的获取JSP Servlet引擎的评估程序线程)。

What you would probably find better and more within standard procedure would be using "cooperating tags" with BodyTagSupport class extension and return EVAL_BODY_BUFFERED in doStartTag() method to repeat process the body and/or object sharing such as storing retrived data in the application hierarchy of the session or on the session for the user.

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

See oracle j2ee custom tags tutorial for more information.

有关更多信息,请参阅oracle j2ee自定义标记教程。

#1


1  

Just using

<bodycontent>JSP</bodycontent>

is not enough. You should do soimething like

是不足够的。你应该这样做

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

to get inner tags rendered (example is for SimpleTag doTag method).

获取内部标记(示例适用于SimpleTag doTag方法)。

However, in the question's code I see that inner tag is comming from a string which is not rendered as a part of JSP, but just some random string. I do not think you can force JSP translator to parse it.

但是,在问题的代码中,我看到内部标记来自一个字符串,该字符串不是作为JSP的一部分呈现,而只是一些随机字符串。我认为你不能强迫JSP翻译解析它。

You can use regexp in your case or try to redesign your code in a way to have a jsp like this:

您可以在您的情况下使用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>

I whould go with regexp.

我应该用正则表达式。

#2


0  

I would be inclined to change the "architecture of your tagging" in that the data you wish to achieve should not be by tag on the inside of the class as it is "markup" designed for a page(though in obscurity it is possible to get the evaluating program thread of the JSP Servlet engine).

我倾向于改变“标记的体系结构”,因为你希望实现的数据不应该在类的内部标记,因为它是为页面设计的“标记”(虽然在默默无闻中它是可能的获取JSP Servlet引擎的评估程序线程)。

What you would probably find better and more within standard procedure would be using "cooperating tags" with BodyTagSupport class extension and return EVAL_BODY_BUFFERED in doStartTag() method to repeat process the body and/or object sharing such as storing retrived data in the application hierarchy of the session or on the session for the user.

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

See oracle j2ee custom tags tutorial for more information.

有关更多信息,请参阅oracle j2ee自定义标记教程。