0

動的コンテンツ (ネストされたタグ) に埋め込まれたカスタム タグがレンダリングされません。

javabean から動的コンテンツを取得し、オブジェクトのリストをカスタム タグに渡して html に処理するページがあります。各オブジェクト内には、レンダリングしたい 2 番目のカスタム タグを含む、出力される 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>

これがプロセッサとプリプロセッサに関係していることは知っていますが、これを機能させる方法についてはわかりません。

4

2 に答える 2

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>

私は正規表現を使用する必要があります。

于 2008-09-08T10:12:37.353 に答える
0

ページ用に設計された「マークアップ」であるため、達成したいデータはクラス内のタグによるものであってはならないという点で、「タグ付けのアーキテクチャ」を変更する傾向があります(あいまいな場合は可能ですがJSP サーブレット エンジンの評価プログラム スレッドを取得します)。

標準的な手順の中でおそらくより良く、より多くのことを見つけるのは、 BodyTagSupportクラス拡張を使用して「協調タグ」を使用EVAL_BODY_BUFFEREDし、doStartTag() メソッドで返して、取得したデータをセッションのアプリケーション階層に保存するなど、本体および/またはオブジェクト共有の処理を繰り返すことです。またはユーザーのセッションで。

詳細については、 oracle j2ee カスタム タグのチュートリアルを参照してください。

于 2011-06-30T16:30:58.257 に答える