jsp taglibs的嵌套el变量 [英] nested el variables for jsp taglibs

查看:103
本文介绍了jsp taglibs的嵌套el变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个仅在标记内可用的el变量,以便它类似于:

I'm trying to create an el variable that is only available inside a tag so that it would be something like:

<mytaglib:mytag>
  Foo is ${foo}
</mytaglib:mytag>

我遵循了可以找到的有关创建嵌套el变量的文档,但是$ {foo}在mytag结束后可用.

I've followed the documentation I can find for creating a nested el variable, however, ${foo} is available after the end of mytag.

.tld文件的缩短版本是:

Shortened version of the .tld file is:

<?xml version="1.0" encoding="UTF-8" ?>

<taglib version="2.0" xmlns="http://java.sun.com/xml/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
  <description>My Tags</description>
  <tlib-version>1.0</tlib-version>
  <jsp-version>2.0</jsp-version>
  <short-name>mytaglib</short-name>
  <uri>http://www.example.com/taglibs/mytaglib</uri>

  <tag>
    <name>mytag</name>
    <tag-class>com.example.tags.MyTag</tag-class>
    <body-content>JSP</body-content>

    <variable>
      <name-given>used</name-given>
      <variable-class>java.lang.String</variable-class>
      <scope>NESTED</scope>
    </variable>
  </tag>
</taglib>

然后MyTag执行以下操作(我已经删除了所有不相关的代码):

then MyTag does the following ( I've removed all code that shouldn't be related ):

public class MyTag extends BodyTagSupport
{
  // these are subtags
  private JspFragment below;
  private JspFragment at;

  @Override
  public int doStartTag() throws JspException
  {
    return EVAL_BODY_BUFFERED;
  }

  @Override
  public int doEndTag() throws JspException
  {
    try
    {
      if (aBoolean)
      {
          below.invoke(pageContext.getOut());
      }
      else
      {
          at.invoke(pageContext.getOut());
      }
    }
    catch (IOException e)
    {
      throw new JspException(e);
    }

    return EVAL_PAGE;
  }

  @Override
  public void doInitBody() throws JspException
  {
    pageContext.setAttribute("used", "valueOfUsed");
  }

  @Override
  public int doAfterBody() throws JspException
  {
    try
    {
      bodyContent.writeOut(bodyContent.getEnclosingWriter());
    }
    catch (IOException e)
    {
      throw new JspException(e);
    }

    return SKIP_BODY;
  }
}

我的理解是,doInitBody中的pageContext.setAttribute()应该在嵌套范围内,并且您在doAfterBody中评估实际主体. 尽管已使用"在标记范围之外可用,但似乎应该是这样.

My understanding is that pageContext.setAttribute() from within doInitBody should be in a nested scope and you evaluate the actual body in doAfterBody. That should seem to be the case though as 'used' is available outside the scope of the tag.

一般背景...标签看起来像(简化形式)

General background... tag would look like (simplified form)

<mytaglib:mytag>
  Used is ${used}
</mytaglib:mytag>

<mytaglib:mytag>
  <mytaglib:at>
    You are at your limit, you have used: ${used}
  </mytaglib:at>
  <mytaglib:below>
    You are below your limit, you have used: ${used}
  </mytaglib:below>
</mytaglib:mytag>

如何获取$ {used}范围内的mytag?

How can I get ${used} to be scoped to mytag?

推荐答案

我总是引用JSTL作为我编写的任何标签的模型.因此,我的方法是查看执行类似操作的标记的源代码,然后将其复制. < c:forEach>标记就是一个很好的例子.

I always reference the JSTL as the model for any tags I write. So my approach would be to look at the source code of a tag that does something similar, and copy that. The <c:forEach> tag is a perfect example.

JSTL中的LoopTagSupport类实现接口 javax.servlet.jsp.tagext.TryCatchFinally ,并在doFinally方法中手动调用pageContext.removeAttribute().

The LoopTagSupport class in JSTL implements the interface javax.servlet.jsp.tagext.TryCatchFinally, and in the doFinally method it calls pageContext.removeAttribute() manually.

引用JSTL1.1源分发:

Quoting the JSTL1.1 source distribution:

/**
 * Removes any attributes that this LoopTagSupport set.
 *
 * <p> These attributes are intended to support scripting variables with
 * NESTED scope, so we don't want to pollute attribute space by leaving
 * them lying around.
 */
public void doFinally() {
    /*
     * Make sure to un-expose variables, restoring them to their
     * prior values, if applicable.
     */
    unExposeVariables();
}

/**
 * Removes page attributes that we have exposed and, if applicable,
 * restores them to their prior values (and scopes).
 */
private void unExposeVariables() {
    // "nested" variables are now simply removed
if (itemId != null)
        pageContext.removeAttribute(itemId, PageContext.PAGE_SCOPE);
if (statusId != null)
    pageContext.removeAttribute(statusId, PageContext.PAGE_SCOPE);
}

这篇关于jsp taglibs的嵌套el变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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