JSTL访问哈希表中的整数/长整型键 [英] JSTL Access Integer/Long key in Hash Map

查看:152
本文介绍了JSTL访问哈希表中的整数/长整型键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用JSTL中的EL时遇到了一些问题,并且无法像我想的那样访问Java哈希表。我知道在EL中,如果Integer被访问为Long。

  Map< Long,Object>我有以下散列映射定义,我试图在JSTL中访问 - testMap = new HashMap< Long,Object>(); 

在JSP页面中,我需要检查映射是否包含特定键。我试图通过检查如果不是空的如下 -

 < c:if test ='$ {!空的testMap [currObj.currVal]}'> 

我也可以在下面的代码中访问某个键的映射值 -

 < c:if test ='$ {testMapMap [5] .data =='something'}'> 

现在的问题 -



将上面的映射定义为< Integer,Object> ,那么第一个c:if工作但第二个失败(因为第二个尝试访问它为Long)。但是,如果我将上面的映射定义为< Long,Object> ,那么第一个if检查总是失败,因为它总是将其识别为空,但是第二个if语句检查该值有效。

有没有什么好方法可以确保我正确访问两个if语句的HashMap?

您是否可以将 currVal 成员重新定义为 Long (或 long >)


$ b $数字文字(匹配 IntegerLiteral 产品在EL语法中)将被表示为 Long 。表达式 currObj.currVal 的计算结果为整数 Long 从来没有 equals() an 整数,所以一个表达式必须导致不同的类型。



实质上,您需要的是明确的类型转换。这样的东西没有内置到EL中,但是您可以创建一个自定义EL功能来为您完成。这是一个您在Java中实现的静态函数,然后在TLD中进行描述。我的另一个答案是给出了一个包装示例。这是你的情况下函数和它的用法。

  package com.y.taglib.core; 

public final class CoercionUtil {

public static Long toLong(Long n){
return n;
}

}

TLD看起来像这样:

 < taglib xmlns =http://java.sun.com/xml/ns/j2eexmlns:xsi = http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns /j2ee/web-jsptaglibrary_2_0.xsdversion =2.0> 
< tlib-version> 1.0< / tlib-version>
<短名称> x-c< /短名称>
< uri> http://dev.y.com/taglib/core/1.0< / uri>
<函数>
< description>强制为一个java.lang.Long。< / description>
< display-name>长< / display-name>
<名称>长< /名称>
< function-class> com.y.taglib.core.CoercionUtil< / function-class>
<函数签名> java.lang.Long toLong(java.lang.Long)< / function-signature>
< / function>
< / taglib>

在您的JSP中:

 <%@ taglib uri =http://dev.y.com/taglib/core/1.0prefix =my%> 
...

JSP引擎负责必要的类型强制转换(从 Integer currVal toLong()所需的 Long 方法,您的方法只是简单地指明所需的类型;如果没有它,JSP引擎会看到 testMap.get(Object)参数的(已擦除) code>,并且自从 Integer 是-a Object 之后没有看到需要执行任何强制操作。

I am facing some problem in using EL in JSTL and not able to access Java Hash Map as I would like. I am aware that in EL the key, if Integer gets accessed as Long. I have following hash map definition that I am trying to access in JSTL -

Map<Long, Object> testMap = new HashMap<Long, Object>();

In JSP page, I need to check if the map contains a specific key or not. I attempt to do that by checking if not empty as following -

<c:if test='${ ! empty testMap[currObj.currVal]}'>

I also access the map's value for a key somewhere in the code like below -

<c:if test='${ testMapMap[5].data == 'something'}'>

Now the problem -

If i define my map above as <Integer, Object> then the first c:if works but second fails (as the second tries to access it as Long). However, if I define my map above as <Long, Object> the first if check always fails as it always recognizes it as empty but the second if statement where I check for the value works.

Is there any good way to make sure I access HashMap for both the if statements correctly? I will appreciate opinions.

解决方案

What is currObj? Can you redefine its currVal member as a Long (or long)?


A numeric literal (matching the IntegerLiteral production in the EL syntax) will be represented as a Long. The expression currObj.currVal evaluates to an Integer. A Long never equals() an Integer, so one expression must result in a different type.

Essentially, what you need is an explicit type conversion. Nothing like this is built into EL, but you could create a custom EL function to do it for you. This is a static function that you implement in Java, then describe in a TLD. Another answer of mine gives an example of the packaging. Here's what the function and its usage could look like in your case.

package com.y.taglib.core;

public final class CoercionUtil {

  public static Long toLong(Long n) {
    return n;
  }

}

The TLD would look like this:

<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0">
  <tlib-version>1.0</tlib-version>
  <short-name>x-c</short-name>
  <uri>http://dev.y.com/taglib/core/1.0</uri>
  <function>
    <description>Coerce to a java.lang.Long.</description>
    <display-name>long</display-name>
    <name>long</name>
    <function-class>com.y.taglib.core.CoercionUtil</function-class>
    <function-signature>java.lang.Long toLong(java.lang.Long)</function-signature>
  </function>
</taglib>

In your JSP:

<%@taglib uri="http://dev.y.com/taglib/core/1.0" prefix="my" %>
...
<c:if test='${ ! empty testMap[my:long(currObj.currVal)]}'>

The JSP engine takes care of the necessary type coercion (from the Integer result of currVal to the Long required by the toLong() method. Your method is there simply to indicate the required type; without it, the JSP engine sees the (erased) type of the argument of testMap.get(Object), and doesn't see the need to perform any coercion since Integer is-an Object.

这篇关于JSTL访问哈希表中的整数/长整型键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆