访问映射<枚举 、对象>在JSTL枚举>
我有:
public enum MyEnum{
One, Two, Three
}
从控制器中,我放入模型:
HashMap<MyEnum, Long> map = new HashMap<MyEnum, Long>();
map.put(MyEnum.One, 1L);
mav.addObject( "map", map);
如何在 JSTL 中以一种简洁的方式访问键枚举 MyEnum.One 的映射中的对象?
${map['One']} //does not seem to work...
也不
${map[MyEnum.One]}
I have:
public enum MyEnum{
One, Two, Three
}
From controller, I put in the model:
HashMap<MyEnum, Long> map = new HashMap<MyEnum, Long>();
map.put(MyEnum.One, 1L);
mav.addObject( "map", map);
How do I in my JSTL access the object in the map for key enum MyEnum.One, in a neat way?
${map['One']} //does not seem to work...
nor does
${map[MyEnum.One]}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
虽然你不能做到这一点并不完全正确,但解决方案并不完全简单。问题是 EL 不会将您作为映射键传入的字符串转换为相应的枚举,因此将 ${map['One']} 放入映射查找中不会使用枚举常量 MyEnum.One。
我遇到了同样的问题,并且不想恢复使用字符串键控映射,因此 JSTL 面临的挑战是如何获取在映射查找中使用的实际枚举引用。
所需要的是将 Enum 常量放入 JSP 的范围中,以便您可以使用实际的 Enum 本身作为键。为此,在控制器中执行如下操作:
您在此处所做的是将变量添加到名为枚举的字符串表示形式的范围中。 (当然,您可以通过在 e.toSring() 前面添加一些值来避免命名问题)
现在,当您执行以下操作时,
您将使用实际的枚举常量作为键,因此将从映射中返回正确的对应值。 (请注意,ONE 周围没有引号,这是因为您在本例中引用了上面添加的请求属性 ONE)
It's not exactly true that you can't do it, but the solution isn't completely straight forward. The issue is EL is not converting the string you pass in as the map key to the corresponding enum for you, so putting ${map['One']} does not use the enum constant MyEnum.One in the map lookup.
I ran into the same issue and didn't want to revert to going with a String keyed map, so the challenge then was in JSTL how to get the actual enum reference to use in the map lookup.
What is required is to get the Enum constants into the scope of the JSP so that you can then use the actual Enum itself as the key. To do this, in the controller you do something like this:
What you've done here is added variables into the scope named as the string representation of the enum. (you could of course avoid naming issues by prepending the e.toSring() with some value)
Now, when you do the following
You will be using the actual enum constant as the key and will therefore get back the proper corresponding value from the map. (notice there are no quotes around ONE, that is because you are referencing the request attribute ONE in this case, that was added above)
你不能。您最好的选择是更改您的映射以使用 enum.name() 作为键:
您的第一种方法将起作用:
或者,如果您不能/不想,您可以编写一个自定义 EL 函数来为您执行上述操作更改地图。
You can't. Your best bet is to change your map to use enum.name() as key:
Your first approach would work then:
Or you can write a custom EL function to do the above for you if you can't / don't want to change the map.
我通常使用这个解决方案:
首先,我导入枚举。然后,我将想要的枚举值保存到 JSTL 变量中。然后我可以用这个变量作为键来访问地图。
I usually use this solution:
First, I import the enum. Then, I save the enum value I want into JSTL variable. Then I can access the map with this variable as the key.
这对我有用。但是你必须写下类的完整名称:my.package.MyEnum 或导入 MyEnum 类:
It works for me. But you have to write the complete name of your class: my.package.MyEnum or to import MyEnum class: