如何制作使用其他JSP标签的自定义JSP标签? [英] How can I make a custom JSP tag that uses other JSP tags?

查看:62
本文介绍了如何制作使用其他JSP标签的自定义JSP标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个自定义JSP标记,其输出包括其他JSP标记,这些标记本身也应动态评估.但是显然我的TagSupport子类写到pageContext.getOut()的所有内容都直接进入了客户端,而没有任何进一步的评估.

I would like to write a custom JSP tag whose output includes other JSP tags which should themselves also be dynamically evaluated. But apparently everything that my TagSupport subclass writes to pageContext.getOut() just goes straight to the client without any further evaluation.

我觉得这应该非常简单,因为这似乎是要使用自定义标签的第一件事:封装和重用其他自定义标签,避免代码重复.

I have a feeling this should be very simple, since it seems like one of the very first things one would want to use custom tags for: encapsulating and reusing other custom tags, avoiding code duplication.

如何使以下代码执行其明显想要执行的操作?:

How do I make the following code do what it obviously wants to do?:

public class MyTag extends TagSupport {
    public int doStartTag() throws JspException {
        try {
            pageContext.getOut().println(
              "The output from this tag includes other tags " +
              "like <mypackage:myOtherTag>this one</mypackage:myOtherTag> " +
              "which should themselves be evaluated and rendered."
            )
        } catch (IOException e) {
            throw new JspException(e);
        }
        return SKIP_BODY;
    }   
}

如果有帮助,请对我的特定用例有一些了解.我有一个自定义标签<user>,它以一种对我的应用程序有用的方式动态呈现用户名(将鼠标悬停在名字,姓氏,电话号码等上).我现在正在编写另一个标记<comment>来显示用户评论,并且我想使用现有的<user>标记在<comment>标记的输出中呈现用户名.

Some background on my particular use case, if it helps. I have a custom tag <user> which dynamically renders a user name in a way that is useful for my application (mouse-hover for first name, last name, phone number, etc.). I'm now writing another tag <comment> for displaying user comments, and I would like to use my existing <user> tag for rendering user names in the output of the <comment> tag.

推荐答案

您可以将您的类分为标记类和tagRenderer类.

You could split your classes into a tag class and a tagRenderer class.

根据您的情况,将有两个新的类分别称为CommentTagRendererUserTagRenderer.

In your situation there would be two new classes called CommentTagRenderer and UserTagRenderer.

以下是新的CommentTag

public int doStartTag() throws JspException {
    JspWriter out = pageContext.getOut(); 
    Comment comment = getComment();
    User user =  getUser();

    CommentTagRenderer commentRenderer = new CommentTagRenderer(out);
    UserTagRenderer userRenderer = new UserTagRenderer(out);

    try {
        commentRenderer.renderComment(comment);
        userRenderer.renderUser(user);          
    } catch (IOException e) {
        //some error handling
    }
    return SKIP_BODY;
  }

这是CommentTagRenderer

private Writer out;
public CommentTagRenderer(Writer out) {
    this.out = out;
}

public void renderComment(Comment comment) throws IOException {
    out.write("<div>");
    out.write(comment.getComment());
    out.write("</div>");
}

这篇关于如何制作使用其他JSP标签的自定义JSP标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆