如何使动态属性在 JSP 标记文件中工作?

发布于 2024-07-17 05:25:31 字数 614 浏览 6 评论 0原文

因此,根据我的 JSP 参考书以及我在网上可以找到的所有其他参考资料,我应该能够执行以下操作:

<%@ tag dynamic-attributes="dynamicAttributesVar" %>

然后当有人使用我未在属性指令中定义的属性时,我应该能够从“dynamicAttributesVar”映射访问该属性:

<%= dynamicAttributesVar.get("someUnexpectedAttribute") %>

但是,这根本不起作用; 当我尝试时,我只是收到“dynamicAttributesVar 无法解析”错误。

现在,我确实发现(通过查看生成的标签的 Java 类)我可以通过以下方式“破解”工作动态属性变量:

<% Map dynamicAttributesVar = _jspx_dynamic_attrs; %>

现在,除非我也在我的应用程序上使用动态属性参数,否则该破解不起作用。 tag 指令,因此参数似乎正在执行某些操作。

但我想知道的是,我怎样才能让它像其他 JSP 用户那样做呢?

So according to my JSP reference book, as well as every other reference I can find on the web, I'm supposed to be able to do something like:

<%@ tag dynamic-attributes="dynamicAttributesVar" %>

and then when someone uses an attribute that I didn't define in an attribute directive, I should be able to access that attribute from the "dynamicAttributesVar" map:

<%= dynamicAttributesVar.get("someUnexpectedAttribute") %>

However, that doesn't work, at all; I just get a "dynamicAttributesVar cannot be resolved" error when I try.

Now, I did discover (by looking at the generated Java class for the tag) that I can "hack" a working dynamic attributes variable by doing:

<% Map dynamicAttributesVar = _jspx_dynamic_attrs; %>

Now, that hack doesn't work unless I also use the dynamic-attributes parameter on my tag directive, so it seems that the parameter is doing something.

But what I want to know is, how can I make it do what it does for every other JSP user out there?

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

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

发布评论

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

评论(2

硪扪都還晓 2024-07-24 05:25:31

只是想通过回答四年前的问题来获得徽章。

我也遇到了这个问题,并在 O'Reilly 使用 JSTL 而不是 scriptlet。

原始发布者可以使用此代码来获取所有键/值:

<c:forEach items="${dynamicAttributesVar}" var="a"> 
${a.key}="${a.value}" 
</c:forEach> 

这将获取特定值:

<c:out value="${dynamicAttributesVar['someUnexpectedAttribute']}"/>

Just trying to get a badge for answering a four year old question.

I have this problem as well and came across some help at O'Reilly to use JSTL instead of scriptlets.

The original poster could have used this code to get all keys/values:

<c:forEach items="${dynamicAttributesVar}" var="a"> 
${a.key}="${a.value}" 
</c:forEach> 

This would get a specific value:

<c:out value="${dynamicAttributesVar['someUnexpectedAttribute']}"/>
来日方长 2024-07-24 05:25:31

“dynamicAttributesVar”不是动态属性放入的页面上下文中的键的名称吗? 所以你可以这样做

<c:out value="${dynamicAttributesVar.someUnexpectedAttributes}"/>

,或者如果你必须使用 scriptlet:(

Map dynamicAttributes = (Map) pageContext.getAttribute("dynamicAttributesVar")

免责声明:我还没有尝试过,我只是在带有直接 Java 实现的标签中使用了动态属性......但这似乎是合理的)

Isn't "dynamicAttributesVar" the name of the key in the page context that the dynamic attributes are put into? So you could do

<c:out value="${dynamicAttributesVar.someUnexpectedAttributes}"/>

or if you must use scriptlets:

Map dynamicAttributes = (Map) pageContext.getAttribute("dynamicAttributesVar")

(Disclaimer: I haven't tried it, I've just used dynamic attributes in tags with direct Java implementations... but it seems reasonable)

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