在传递给 JSP 标记处理程序之前评估变量

发布于 2024-07-18 03:48:17 字数 1153 浏览 5 评论 0原文

当尝试使用自定义 JSP 标记库时,我在 JSP 中定义了一个变量,希望在传递到标记库之前对其进行评估。 但是,我似乎无法让它发挥作用。 这是我的 JSP 的简化版本:

<% int index = 8; %>

<foo:myTag myAttribute="something_<%= index %>"/>

TagHandlerdoStartTag() 方法使用 pageContext 的输出流根据输入的属性进行写入:

public int doStartTag() {
    ...
    out.println("Foo: " + this.myAttribute);
}

但是,我在我的最终标记是:

Foo: something_<%= index %>

而不是我想要的:

Foo: something_8

我的属性的标记库定义是:

<attribute>
    <name>myAttribute</name>
    <required>true</required>
</attribute>

我尝试使用 rtexprvalue 以及 truefalse 配置属性,但都不起作用。 有没有办法可以配置该属性,以便在将其发送到处理程序之前对其进行评估? 还是我的做法完全错误?

我对 JSP 标签比较陌生,因此我愿意接受解决此问题的替代方案。 另外,我意识到在 JSP 中使用 scriptlet 是不受欢迎的,但我在这里使用一些遗留代码,所以我现在有点坚持使用它。

编辑:

我也尝试过:

<foo:myTag myAttribute="something_${index}"/>

这也不起作用 - 它只是输出something_${index}

When trying to use a custom JSP tag library, I have a variable defined in JSP that I would like to be evaluated before being passed to the tag library. However, I cannot seem to get it to work. Here's a simplified version of my JSP:

<% int index = 8; %>

<foo:myTag myAttribute="something_<%= index %>"/>

The doStartTag() method of my TagHandler uses the pageContext's output stream to write based on the inputted attribute:

public int doStartTag() {
    ...
    out.println("Foo: " + this.myAttribute);
}

However, the output I see in my final markup is:

Foo: something_<%= index %>

instead of what I want:

Foo: something_8

My tag library definition for the attribute is:

<attribute>
    <name>myAttribute</name>
    <required>true</required>
</attribute>

I have tried to configure the attribute with rtexprvalue both true and false, but neither worked. Is there a way I can configure the attribute so that it's evaluated before being sent to the Handler? Or am I going about this totally wrong?

I'm relatively new to JSP tags, so I'm open to alternatives for solving this problem. Also, I realize that using scriptlets in JSP is frowned upon, but I'm working with some legacy code here so I'm kind of stuck with it for now.

Edit:

I have also tried:

<foo:myTag myAttribute="something_${index}"/>

which does not work either - it just outputs something_${index}.

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

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

发布评论

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

评论(3

愛放△進行李 2024-07-25 03:48:17

我不认为您可以在自定义标记的属性中使用 <%= ... %> ,除非您的 <%= ... %> 是属性值的全部内容。 以下内容对您有用吗?

<% int index = 8; %>
<% String attribValue = "something_" + index; %>

<foo:myTag myAttribute="<%= attribValue %>"/>

编辑:我相信自定义标记属性中的 <% ... %> 只能包含变量名称。 不是任何 Java 表达式。

I don't believe that you can use a <%= ... %> within an attribute in a custom tag, unless your <%= ... %> is the entire contents of the attribute value. Does the following work for you?

<% int index = 8; %>
<% String attribValue = "something_" + index; %>

<foo:myTag myAttribute="<%= attribValue %>"/>

EDIT: I believe the <% ... %> within the custom tag attribute can only contain a variable name. not any Java expression.

百合的盛世恋 2024-07-25 03:48:17

为了保持 JSP 代码干净整洁,请尽可能避免编写脚本。 我认为这是首选方法:

<foo:myTag >
  <jsp:attribute name="myAttribute">
    something_${index}
  </jsp:attribute>
</foo:myTag >

如果您的标签还包含正文,则必须将其从 更改

<foo:myTag myAttribute="<%= attribValue %>">
  body
</foo:myTag >

<foo:myTag >
  <jsp:attribute name="myAttribute">
    something_${index}
  </jsp:attribute>
  <jsp:body>
    body
  </jsp:body>
</foo:myTag >

To keep your JSP code clean and neat, avoid scripting when possible. I think this is the preferred way to do it:

<foo:myTag >
  <jsp:attribute name="myAttribute">
    something_${index}
  </jsp:attribute>
</foo:myTag >

if your tag also contains a body, you'll have to change it from

<foo:myTag myAttribute="<%= attribValue %>">
  body
</foo:myTag >

to

<foo:myTag >
  <jsp:attribute name="myAttribute">
    something_${index}
  </jsp:attribute>
  <jsp:body>
    body
  </jsp:body>
</foo:myTag >
痴情换悲伤 2024-07-25 03:48:17

<foo:myTag myAttribute="something_${index}"/>

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