7

我一定是盲人或使用了错误的搜索词,因为我找不到一个好的答案。

我有两个自定义 JPS 标记文件。一个将嵌套在另一个中。如何从子标签内部访问父标签中的属性?

一种解决方法是添加一个变量来请求范围,但我不喜欢这个选项,它可能会导致一些问题。有没有更直接的选择?

谢谢!

<%-- OuterTag.tag --%>
--------------------------------------
<%@tag %>
<%@attribute name="color" required="true" %>
<c:set var="color" value="${color}" scope="request" /> <%-- I'm trying to avoid doing this --%>
<div>
   <jsp:doBody/>
</div>


<%-- InnerTag.tag --%>
--------------------------------------
<%@tag %>
<p style="background-color: ${parent.color}"/> <%-- I want to do something like this --%>



<%-- Example Usage --%>
--------------------------------------
<custom:OuterTag color="red">
    <custom:InnerTag/>
    <custom:InnerTag/>
    <custom:InnerTag/>
</custom:OuterTag>
4

2 回答 2

0

您在这里尝试做的事情是完全合乎逻辑的 - 自定义标签和标签文件应该允许我们实现。此行为是基于实现的 - 并且已在 Apache 上修复,请参见此处。你用的是什么容器?我在 Jetty 中遇到了类似的问题,并提交了另一个已确认但尚未解决的错误。

于 2014-09-29T12:38:28.777 回答
0

您可以使用以下方法找到您的父标签:

ParentTag parent = (ParentTag)findAncestorWithClass(this, ParentTag.class);

然后,您可以在父级上使用公共方法,子级可以使用它与之交互。

查看此 URL 了解更多详细信息: http: //www.informit.com/articles/article.aspx?p=26119 &seqNum=9

于 2016-06-03T12:51:29.260 回答