使用 Struts 2 访问 HashMap

发布于 2024-09-28 16:49:10 字数 854 浏览 2 评论 0原文

我有使用 struts2 标签在页面上创建的哈希图。它看起来像这样

<s:set var="mymap" value="#request.mymap"/>

在页面的某个时刻,我需要根据键从哈希图中获取一个值,并且我想使用 OGNL 来完成此操作。

密钥是根据某种逻辑生成的,我使用另一个 标签存储该密钥。像这样

<s:set var="mykey" value="1">

我需要使用这个键从哈希图中获取一个值。我需要显示它。

如何简单地调用哈希图上的 get 函数?

我尝试了这个

<s:property value="#mymap[#mykey]"/>

、这个

<s:property value="#mymap[%{#mykey}]"/>

和这个

<s:property value="%{#mymap[%{#mykey}}]"/>

第三个显然由于嵌套问题而不起作用。
但由于 value 属性的处理方式,相同的嵌套逻辑也适用于第二种情况。然而似乎没有一个对我有用。

这里的问题是我的密钥未知。它是基于某种模式动态生成的字符串。我需要使用这个动态键访问存储在哈希图中的对象。由于无法嵌套 ognl,我陷入了困境。

我想这个问题很简单。我几乎觉得我明白了,但不知怎的,我却找不到解决方案。

I have hashmap that was created on a page using the struts2 <s:set> tag. It looks something like this

<s:set var="mymap" value="#request.mymap"/>

At some point in the page, i need to get a value from the hashmap based upon a key, and i want to do it using OGNL.

The key is generated based upon some logic, which i store using another <s:set> tag. Something like this

<s:set var="mykey" value="1">

I need to get a value from the hashmap using this key. And I need to display it.

How do I simply call the get function on the hashmap?

I tried this

<s:property value="#mymap[#mykey]"/>

and this

<s:property value="#mymap[%{#mykey}]"/>

and this

<s:property value="%{#mymap[%{#mykey}}]"/>

The third one obviously does not work because of the nesting problem.
But the same nesting logic is applicable to the second case as well, due to the manner the value attribute is handled. However none seem to work for me.

The issue here is that my key is unknown. It is a dynamically generated string based upon some pattern. I need to access the object stored in the hashmap using this dynamic key. And due to the inability of nesting ognl, I am in a fix.

I suppose the issue is very simple. I almost feel that I get it, but somehow the solution eludes me.

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

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

发布评论

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

评论(1

黑凤梨 2024-10-05 16:49:10

我想我使用的是不同版本的 struts,其中需要使用 %{} 来评估表达式。我现在更改了 jar 文件。
这就是我的工作:

<s:property value="#mymap.[#mykey2]"/>

我的问题来了,因为我试图在 as:a 标签的 href 中使用它。如果没有 %{} 运算符,则不会计算表达式。

所以,我想,我一开始就是对的。其余的时间,只是我在犯傻。 :>

更新:
我写了一篇关于这个问题的博客文章,以防有人感兴趣。
http://mycodefixes.blogspot.com/2010 /11/struts-2-creating-and-accessing-maps.html

I suppose I was using a different version of struts wherein using the %{} was required for the expression to be evaluated. I changed the jar files now.
This is what did the job for me:

<s:property value="#mymap.[#mykey2]"/>

My problem was coming because I was trying to use it in a href for a s:a tag. And without the %{} operator, the expression was not being evaluated.

So, i guess, i was right in the beginning itself. Rest of the time, it was just me being silly. :>

Update:
I wrote a blog post on the issue, in case anyone is interested.
http://mycodefixes.blogspot.com/2010/11/struts-2-creating-and-accessing-maps.html

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