How can I make a custom JSP tag that uses other JSP tags?

戏子无情 提交于 2019-12-04 09:19:54

问题


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

Edit: 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.


回答1:


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

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

Here is an example of the new 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;
  }

And here is an example of the 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>");
}



回答2:


The easiest way is to write your custome tag as a JSP tag file, rather than in Java. This way, the new tag can use other custom tags easily. Create a file myTag.tag in /WEB-INF/tags, and use the following code:

<%@ tag %>
<%@ attribute name="value" required="true" rtexprvalue="true" type="java.lang.String"%>
<%@ taglib prefix="mypackage" uri="mypackage.tld" %>
The output from this tag includes other tags 
like <mypackage:myOtherTag>${value}</mypackage:myOtherTag>
which should themselves be evaluated and rendered.

More information about tag files here: http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPTags5.html



来源:https://stackoverflow.com/questions/7971617/how-can-i-make-a-custom-jsp-tag-that-uses-other-jsp-tags

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!