Scriptlet 变量不会在自定义 JSP 标记的属性内进行计算

发布于 2024-11-28 12:01:40 字数 892 浏览 0 评论 0 原文

我试图在单击链接时调用 JavaScript 函数。此 JavaScript 函数在 JSP 标记的属性中定义,我尝试将 scriptlet 变量传递给该函数。但是,它不会被评估。代码的相关部分是:

<span>
  <mysecurity:secure_link id='<%="editButton_"+commentUUID%>' entitlement="" 
    actionOnClick="editComment('<%= commentUUID %>');return false;"
    isSurroundedByBrackets="true" enableTitle="" disableLink="<%=disableLink%>">
    <span style="color:#0033BB; font:8pt arial;">
      <bean:message key="button.edit" />
    </span>
   </mysecurity:secure_link>
</span>

IE8在左下角提到了一个JavaScript错误。当我右键单击并查看源代码时,生成的 HTML 为:

onclick="editComment('<%= commentUUID %>');return false;"

因此, <%=commentUUID%> 未在 actionOnClick 属性中进行评估,但已成功已在 id 属性中进行评估。

这是如何引起的以及如何修复它?

I am trying to have a JavaScript function called when I click a link. This JavaScript function is definied in an attribute of a JSP tag and I am trying to pass a scriptlet variable to the function. However, it doesn't get evaluated. The relevant part of the code is:

<span>
  <mysecurity:secure_link id='<%="editButton_"+commentUUID%>' entitlement="" 
    actionOnClick="editComment('<%= commentUUID %>');return false;"
    isSurroundedByBrackets="true" enableTitle="" disableLink="<%=disableLink%>">
    <span style="color:#0033BB; font:8pt arial;">
      <bean:message key="button.edit" />
    </span>
   </mysecurity:secure_link>
</span>

IE8 mentions a JavaScript error in the bottom left corner. When I right click and view source, the generated HTML is:

onclick="editComment('<%= commentUUID %>');return false;"

So, the <%=commentUUID%> is not been evaluated in the actionOnClick attribute, but it's successfully been evaluated in id attribute.

How is this caused and how can I fix it?

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

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

发布评论

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

评论(2

┊风居住的梦幻卍 2024-12-05 12:01:40

我不确定 是自定义的还是现有的第 3 方 JSP 标记库。现代 JSP 标记通常不会评估旧版 scriptlet 表达式。您应该改用EL(表达式语言)

首先确保 commentUUID 变量存储为页面或请求范围的属性,以便 EL 可以使用它,如以下预处理 servlet

request.setAttribute("commentUUID", commentUUID);

在 JSP 中使用另一个 scriptlet

<% request.setAttribute("commentUUID", commentUUID); %>

使用 JSTL在JSP中:

<c:set var="commentUUID"><%=commentUUID%></c:set>

然后在EL中可以如下访问:

<mysecurity:secure_link actionOnClick="editComment('${commentUUID}');return false;" />

I'm not sure whether the <mysecurity:secure_link> is a custom or an existing 3rd party JSP tag library. Modern JSP tags usually do not evaluate legacy scriptlet expressions. You should rather be using EL (Expression Language) instead.

First ensure that commentUUID variable is been stored as an attribute of the page or request scope so that it's available to EL, like as the following example in a preprocessing servlet:

request.setAttribute("commentUUID", commentUUID);

or using another scriptlet in JSP:

<% request.setAttribute("commentUUID", commentUUID); %>

or using JSTL's <c:set> in JSP:

<c:set var="commentUUID"><%=commentUUID%></c:set>

then you can access it as follows in EL:

<mysecurity:secure_link actionOnClick="editComment('${commentUUID}');return false;" />
请恋爱 2024-12-05 12:01:40

根据 @BalusC 的建议,最终对我有用的是使用 editcomment(this.id.split('_')[1]) 。正确的工作代码如下:

<span>
  <mysecurity:secure_link id='<%="editButton_"+commentUUID%>' entitlement="" 
      actionOnClick="javascript:editComment(this.id.split('_')[1]);return false;"
      isSurroundedByBrackets="true" enableTitle="" disableLink="<%=disableLink%>">
      <span style="color:#0033BB; font:8pt arial;">
         <bean:message key="button.edit" />
      </span>
  </mysecurity:secure_link>
</span>

What finally worked for me, with the @BalusC 's advice was to use editcomment(this.id.split('_')[1]). The correct working code is as follows:

<span>
  <mysecurity:secure_link id='<%="editButton_"+commentUUID%>' entitlement="" 
      actionOnClick="javascript:editComment(this.id.split('_')[1]);return false;"
      isSurroundedByBrackets="true" enableTitle="" disableLink="<%=disableLink%>">
      <span style="color:#0033BB; font:8pt arial;">
         <bean:message key="button.edit" />
      </span>
  </mysecurity:secure_link>
</span>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文