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,082,665
  • 372
  • 3,610
  • 3,555
geffchang
  • 3,279
  • 2
  • 32
  • 58

5 Answers5

40

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,082,665
  • 372
  • 3,610
  • 3,555
2

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

lin
  • 1,425
  • 1
  • 19
  • 27
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}" />
Vy Do
  • 46,709
  • 59
  • 215
  • 313
-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