自定义 JSP 标签 - 如何获取标签正文?

发布于 2024-08-25 21:56:58 字数 184 浏览 5 评论 0原文

我有一个像这样的自定义 jsp 标签:

<a:customtag>
    The body of the custom tag...
    More lines of the body...
</a:customtag>

在自定义标签中,如何获取正文的文本?

I have a custom jsp tag like this:

<a:customtag>
    The body of the custom tag...
    More lines of the body...
</a:customtag>

In the custom tag, how can I get the text of what the body is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

土豪我们做朋友吧 2024-09-01 21:56:58

它很复杂,因为有两种机制。

如果您要扩展 SimpleTagSupport,则会得到 getJspBody() 方法。它返回一个 JspFragment,您可以 调用(Writer writer)将正文内容写入writer。

您应该使用 SimpleTagSupport,除非您有特定原因使用 BodyTagSupport(如旧标签支持),因为它更简单。

如果您正在使用经典标记,则可以扩展 BodyTagSupport 并因此可以访问 getBodyContent() 方法。这将为您提供一个 BodyContent 对象,您可以从中检索正文内容。

It's complicated because there are two mechanisms.

If you're extending SimpleTagSupport, you get getJspBody() method. It returns a JspFragment that you can invoke(Writer writer) to have the body content written to the writer.

You should use SimpleTagSupport unless you have a specific reason to use BodyTagSupport (like legacy tag support) as it is - well - simpler.

If you are using classic tags, you extend BodyTagSupport and so get access to a getBodyContent() method. That gets you a BodyContent object that you can retrieve the body content from.

漫雪独思 2024-09-01 21:56:58

如果您使用 jsp 2.0 方法的自定义标记,您可以这样做:

ma​​ke-h1.tag

<%@tag description="Make me H1 " pageEncoding="UTF-8"%>   
<h1><jsp:doBody/></h1>

在 JSP 中使用它:

<%@ taglib prefix="t" tagdir="/WEB-INF/tags"%>
<t:make-h1>An important head line </t:make-h1>

If you are using a custom tag with jsp 2.0 approach, you can do it as:

make-h1.tag

<%@tag description="Make me H1 " pageEncoding="UTF-8"%>   
<h1><jsp:doBody/></h1>

Use it in JSP as:

<%@ taglib prefix="t" tagdir="/WEB-INF/tags"%>
<t:make-h1>An important head line </t:make-h1>
半世晨晓 2024-09-01 21:56:58

扩展 Brabster 的答案,我使用 SimpleTagSupport.getJspBody()JspFragment 写入内部 StringWriter 进行检查和操作:

public class CustomTag extends SimpleTagSupport {
    @Override public void doTag() throws JspException, IOException {
        final JspWriter jspWriter = getJspContext().getOut();
        final StringWriter stringWriter = new StringWriter();
        final StringBuffer bodyContent = new StringBuffer();

        // Execute the tag's body into an internal writer
        getJspBody().invoke(stringWriter);

        // (Do stuff with stringWriter..)

        bodyContent.append("<div class='custom-div'>");
        bodyContent.append(stringWriter.getBuffer());
        bodyContent.append("</div>");

        // Output to the JSP writer
        jspWriter.write(bodyContent.toString());
    }
}

}

To expand on Brabster's answer, I've used SimpleTagSupport.getJspBody() to write the JspFragment to an internal StringWriter for inspection and manipulation:

public class CustomTag extends SimpleTagSupport {
    @Override public void doTag() throws JspException, IOException {
        final JspWriter jspWriter = getJspContext().getOut();
        final StringWriter stringWriter = new StringWriter();
        final StringBuffer bodyContent = new StringBuffer();

        // Execute the tag's body into an internal writer
        getJspBody().invoke(stringWriter);

        // (Do stuff with stringWriter..)

        bodyContent.append("<div class='custom-div'>");
        bodyContent.append(stringWriter.getBuffer());
        bodyContent.append("</div>");

        // Output to the JSP writer
        jspWriter.write(bodyContent.toString());
    }
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文