如何将 EL 表达式作为嵌套 jsp 2.0 标记中的参数?

发布于 2024-07-13 20:47:41 字数 953 浏览 10 评论 0原文

我想做这样的事情来调用 JSP 2.0 标记:

<mytags:foo abc="<%=def%>">
  <mytags:bar ghi="<%=jkl%>"/>
</mytags:foo>

其中字符串 defjkl 在 jsp 文件中定义earielr。 假设我的标签文件如下所示:

foo.tag:

<%@ tag body-content="scriptless" %>
<%@ attribute name="abc" required="true" %>
<div class="${abc}">
  <jsp:doBody/>
</div>

bar.tag:

<%@ tag body-content="scriptless" %>
<%@ attribute name="ghi" required="true" %>
<div>${ghi}</div>

我希望输出如下所示:(

<div class="def">
<div>jkl</div>
</div>

假设变量 defjkl 在调用 JSP 文件中分别初始化为 defjkl。)

外部标记很好地获取其属性(< code>

) 但内部失败。

这可能吗? 我收到 jkl 无法解决的错误。

I want to do something like this to call a JSP 2.0 tag:

<mytags:foo abc="<%=def%>">
  <mytags:bar ghi="<%=jkl%>"/>
</mytags:foo>

Where Strings def and jkl are defined earielr in the jsp file. Suppose my tag files look like this:

foo.tag:

<%@ tag body-content="scriptless" %>
<%@ attribute name="abc" required="true" %>
<div class="${abc}">
  <jsp:doBody/>
</div>

bar.tag:

<%@ tag body-content="scriptless" %>
<%@ attribute name="ghi" required="true" %>
<div>${ghi}</div>

I want the output to look like this:

<div class="def">
<div>jkl</div>
</div>

(assuming the variables def and jkl were initialized to def and jkl, respectively, in the calling JSP file.)

The outer tag gets its attribute just fine (<div class="def">) but the inner one fails.

Is this possible? I am getting errors that jkl cannot be resolved.

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

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

发布评论

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

评论(1

一场信仰旅途 2024-07-20 20:47:41

请注意标记指令中的 body-content="scriptless" 属性。 这意味着标记包围的主体不能包含 scriptlet 代码(<% %> 内容)。 你需要使用EL。

如果我将 JSP 中的内容更改为:

  <c:set var="def" value="def"></c:set>
  <c:set var="jkl" value="jkl"></c:set>

  <mytags:foo abc="${def}">
      <mytags:bar ghi="${jkl}"/>
  </mytags:foo>

请注意,我需要添加
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

Note the body-content="scriptless" attribute in the tag directive. This means that the body surrounded by the tag can't contain scriptlet code (the <% %> stuff). You need to use EL.

It works for me if I change the stuff in the JSP to:

  <c:set var="def" value="def"></c:set>
  <c:set var="jkl" value="jkl"></c:set>

  <mytags:foo abc="${def}">
      <mytags:bar ghi="${jkl}"/>
  </mytags:foo>

Note that I need to add
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

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