在传递给 JSP 标记处理程序之前评估变量
当尝试使用自定义 JSP 标记库时,我在 JSP 中定义了一个变量,希望在传递到标记库之前对其进行评估。 但是,我似乎无法让它发挥作用。 这是我的 JSP 的简化版本:
<% int index = 8; %>
<foo:myTag myAttribute="something_<%= index %>"/>
TagHandler
的 doStartTag()
方法使用 pageContext 的输出流根据输入的属性进行写入:
public int doStartTag() {
...
out.println("Foo: " + this.myAttribute);
}
但是,我在我的最终标记是:
Foo: something_<%= index %>
而不是我想要的:
Foo: something_8
我的属性的标记库定义是:
<attribute>
<name>myAttribute</name>
<required>true</required>
</attribute>
我尝试使用 rtexprvalue
以及 true
和 false 配置属性
,但都不起作用。 有没有办法可以配置该属性,以便在将其发送到处理程序之前对其进行评估? 还是我的做法完全错误?
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不认为您可以在自定义标记的属性中使用
<%= ... %>
,除非您的<%= ... %>
是属性值的全部内容。 以下内容对您有用吗?编辑:我相信自定义标记属性中的
<% ... %>
只能包含变量名称。 不是任何 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?EDIT: I believe the
<% ... %>
within the custom tag attribute can only contain a variable name. not any Java expression.为了保持 JSP 代码干净整洁,请尽可能避免编写脚本。 我认为这是首选方法:
如果您的标签还包含正文,则必须将其从 更改
为
To keep your JSP code clean and neat, avoid scripting when possible. I think this is the preferred way to do it:
if your tag also contains a body, you'll have to change it from
to
<foo:myTag myAttribute="something_${index}"/>