访问嵌套 JSP 标记之间的变量

发布于 2024-08-27 22:20:11 字数 608 浏览 2 评论 0原文

我想在两个嵌套的 JSP tagx 工件之间交换信息。举个例子:

list.jspx

<myNs:table data="${myTableData}">
    <myNs:column property="firstName" label="First Name"/>
    <myNs:column property="lastName" label="Last Name"/>
</myNs:table>

现在,table.tagx 应该显示嵌套列标签中定义的数据列。问题是如何从表标签访问嵌套列标签的 property 和 label 属性的值。我尝试了 jsp:directive.variable 但这似乎只能在 jsp 和标签之间交换信息,而不能在嵌套标签之间交换信息。

请注意,我想完全避免对表和列标记使用 java 支持对象。

我还想知道如何访问由父标记定义的属性(在本示例中,我想从 column.tagx 访问 table.tagx 中的数据属性的内容)。

所以它归结为如何访问嵌套 JSP 标签之间的变量,这些变量纯粹是通过标签定义本身实现的(不需要 Java TagHandler 实现)?

I would like to exchange information between two nested JSP tagx artifacts. To give an example:

list.jspx

<myNs:table data="${myTableData}">
    <myNs:column property="firstName" label="First Name"/>
    <myNs:column property="lastName" label="Last Name"/>
</myNs:table>

Now, the table.tagx is supposed to display the data columns as defined in the nested column tags. The question is how do I get access to the values of the property and label attributes of the nested column tags from the table tag. I tried jsp:directive.variable but that seems only to work to exchange information between a jsp and a tag, but not between nested tags.

Note, I would like to avoid using java backing objects for both the table and the column tags at all.

I would also like to know how I can access an attribute defined by a parent tag (in this example I would like to access the contents of the data attribute in table.tagx from column.tagx).

So it boils down to how can I access variables between nested JSP tags which are purely implemented through the tag definitions themselves (no Java TagHandler implementation desired)?

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

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

发布评论

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

评论(1

や莫失莫忘 2024-09-03 22:20:11

这个想法是在请求范围内共享数据:

  1. 在 myNs:table 中创建一个请求范围的占位符变量来保存数据(在您的情况下,您将需要其中两个:一个用于属性,另一个用于标签):

    
    
    
  2. 然后通过 以便列可以填充占位符。
  3. 在 myNs:column 中填充占位符,记住将它们保留在请求范围内:

    <前><代码>;

    >


  4. 现在,在 myNs 中调用 后:table 你已经填充了值,你所需要的就是使用逗号作为分隔符来分割字符串,然后做你想做的事情:

    <前><代码><表>;
    <头>





PS: 功劳归于 Spring Roo 的人,看看他们的 表。 tagxcolumn.tagx

The idea is to share the data in request scope:

  1. In myNs:table create a request scoped placeholder variable to hold the data (in your case you will need two of them: one for properties and another for labels):

    <c:set var="properties" scope="request" />
    <c:set var="labels" scope="request" />
    
  2. Then invoke your column tags via <jsp:doBody /> so that columns could populate the placeholders.
  3. In myNs:column populate the placeholders, remember to keep them in request scope:

    <c:choose>
      <c:when test="${empty properties}" scope="request">
        <c:set var="properties" value="${property}" scope="request" />
      </c:when>
      <c:otherwise>
        <c:set var="properties" value="${properties},${property}" scope="request" />
      </c:otherwise>
    </c:choose>
    
  4. Now, after you invoked <jsp:doBody /> in your myNs:table you got the values populated, all you need is to split the string using comma as a separator and then do whatever you want:

    <table>
      <thead>
        <tr>
          <c:forTokens items="${labels}" delims="," var="label">
            <th><c:out value="${label}" /></th>
          </c:forTokens>
      </thead>
    </table>
    

P.S.: The credits go to Spring Roo guys, take a look at their table.tagx and column.tagx.

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