2011-02-10 40 views
1

大家好 我有访问一个HashMap中到使用EL和JSTL 我的HashMap的是,像这样在servlet我JSL一个问题:如何在jsp中使用JSTL-EL访问HashMap?

HashMap indexes=new HashMap(); 

然后让想我添加的财产以后,如:

indexes.put(1,"Erik") 

然后我把它添加到会话:session.setAttribute("indexes",indexes)

从JSP,如果我像这样访问

HashMap中
${sessionScope.indexes} 

它显示在地图中的所有键值对,但像这样的例子:

${sessionScope.indexes[1]} or ${sessionScope.indexes['1']} 

它不会工作

,据我可以看到这是在很多使用的ethod教程我不知道我在哪里失败 任何建议?

回答

3

在EL中将数字视为Long。你的情况:

HashMap indexes = new HashMap(); 
indexes.put(1, "Erik"); // here it autobox 1 to Integer 

和JSP

${sessionScope.indexes[1]} // will search for Long 1 in map as key so it will return null 
${sessionScope.indexes['1']} // will search for String 1 in map as key so it will return null 

因此,要么你可以让你的地图的关键是长或字符串使用。

Map<Long, String> indexes = new HashMap<Long, String>(); 
indexes.put(1L, "Erik"); // here it autobox 1 to Long 

${sessionScope.indexes[1]} // will look for Long 1 in map as key 

Map<String, String> indexes = new HashMap<String, String>(); 
indexes.put("1", "Erik"); 

${sessionScope.indexes['1']} // will look for String 1 in map as key 
+0

嗨Jigar THX的意见,但它不工作。 – JBoy 2011-02-10 06:28:56

相关问题