19

How do I correct this statement:

${model.myHashtable[model.data.id]}.

myHashtable is defined as

Hashtable<String, String>

But, ${model.data.id} returns an int.

I tried to do something like

${model.myHashtable['model.data.id']}

But it does not work. Any other ideas, aside from changing the type of id to String?

BalusC
  • 1,040,783
  • 362
  • 3,548
  • 3,513
geffchang
  • 3,226
  • 2
  • 30
  • 53

5 Answers5

39

Set it as body of <c:set>. It will implicitly be converted to String.

<c:set var="idAsString">${model.data.id}</c:set>
<c:out value="${model.myHashtable[idAsString]}" />
BalusC
  • 1,040,783
  • 362
  • 3,548
  • 3,513
2

${''.concat(model.data.id)} works for me,you may try it.

Chaojun Zhong
  • 1,180
  • 1
  • 17
  • 25
0

you can use org.apache.commons.collections4.map.CaseInsensitiveMap both String "1" and int 1 are the same key;

0

In JSP file:

<%@ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>

<!-- lines of code... -->

<fmt:parseNumber var = "id_value" type = "number" value = "${model.data.id}" />
<c:out value = "${id_value}" />
James Graham
  • 39,063
  • 41
  • 167
  • 242
-2

The $ {model.data.id} returns an integer, use Hashtable <Integer, String>, which solves my problem. If the id is a number, then <c:set> is useless.

JamesR
  • 1